

{"id":4444,"date":"2017-10-12T09:51:26","date_gmt":"2017-10-12T04:21:26","guid":{"rendered":"http:\/\/data-flair.training\/blogs\/?p=4444"},"modified":"2021-08-25T17:26:33","modified_gmt":"2021-08-25T11:56:33","slug":"r-factor-functions","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/","title":{"rendered":"R Factor &#8211; Learn the complete process from Creation to Modification!"},"content":{"rendered":"<p>R factor is used to store\u00a0categorical data as levels. It can store both character and integer types of data. These factors are created with the help of<em> factor()<\/em> functions, by taking a vector as input.<\/p>\n<p>So, why waste time? Let&#8217;s start the process of <em>creating factors, accessing its components, generating factor level and modification<\/em> with the help of examples.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-55786 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg\" alt=\"Learn R Factor from Strach\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>What is R Factor?<\/h2>\n<p>R factors are variables. It takes a limited number of different values. Hence, those variables are often known as <em>categorical variables<\/em>. In order to categorise the data and store it on multiple levels, we use the data object called R factor. They can store both strings and integers. They are also useful in the columns which have a limited number of unique values.<\/p>\n<p>The factor is stored as integers. They have labels associated with these unique integers. We need to be careful while treating factors like strings. Factor contains a predefined set value called levels. By default, R always sorts levels in alphabetical order.<\/p>\n<p><em><strong>Before proceeding ahead, let&#8217;s first revise the <a href=\"https:\/\/data-flair.training\/blogs\/r-vector\/\">R Vector Operations<\/a><\/strong><\/em><\/p>\n<h3>Attributes of a Factor<\/h3>\n<p>Some important attributes of the factor that we will use in this article are:<\/p>\n<ul>\n<li><strong>x:<\/strong> The input vector that is to be transformed into a vector.<\/li>\n<li><strong>levels:<\/strong> This is an optional vector that represents a set of unique values that are taken by x.<\/li>\n<li><strong>labels<\/strong>: It is a character vector that corresponds to the number of labels.<\/li>\n<li><strong>Exclude<\/strong>: With this attribute, we specify the values to be excluded.<\/li>\n<li><strong>ordered<\/strong>: This is a logical attribute that determines if the levels should be ordered.<\/li>\n<li><strong>nmax<\/strong>: This attribute specifies the upper bound for the maximum number of levels.<\/li>\n<\/ul>\n<h3>How to Create an R Factor<\/h3>\n<p>In order to create your first R factor, we make use of the<em> factor()<\/em> function.<\/p>\n<p>To create our factor, we first create a vector called \u2018directions\u2019 that holds the direction \u201cNorth\u201d, \u201cWest\u201d and \u201cSouth\u201d. Notice how \u201cEast\u201d is absent from this vector.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; #Author DataFlair\r\n&gt; directions &lt;- c(\"North\", \"North\", \"West\", \"South\")<\/pre>\n<p>Let us convert this vector into a factor using the <em>factor()<\/em> function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">factor(directions)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-create-Factor-in-R.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55767\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-create-Factor-in-R.png\" alt=\"Creating R Factor with Example\" width=\"1301\" height=\"740\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-create-Factor-in-R.png 1301w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-create-Factor-in-R-150x85.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-create-Factor-in-R-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-create-Factor-in-R-768x437.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-create-Factor-in-R-1024x582.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-create-Factor-in-R-520x296.png 520w\" sizes=\"auto, (max-width: 1301px) 100vw, 1301px\" \/><\/a><\/p>\n<p>Notice that in the output, we have three levels \u201cNorth\u201d, \u201cSouth\u201d and \u201cWest\u201d. This does not contain the redundant \u201cNorth\u201d that was occurring twice in our input vector.<\/p>\n<p><strong>In order to add this missing level to our factors, we use the \u201clevels\u201d attribute as follows:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">factor(directions, levels= c(\"North\", \"East\", \"South\", \"West\"))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Levels-in-R-Factor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55768\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Levels-in-R-Factor.png\" alt=\"Defining levels in Factors\" width=\"1298\" height=\"743\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Levels-in-R-Factor.png 1298w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Levels-in-R-Factor-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Levels-in-R-Factor-300x172.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Levels-in-R-Factor-768x440.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Levels-in-R-Factor-1024x586.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Levels-in-R-Factor-520x298.png 520w\" sizes=\"auto, (max-width: 1298px) 100vw, 1298px\" \/><\/a><\/p>\n<p><strong>In order to provide abbreviations or \u2018labels\u2019 to our levels, we make use of the labels argument as follows &#8211;<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">factor(directions, levels= c(\"North\", \"East\", \"South\", \"West\"), labels=c(\"N\", \"E\", \"S\", \"W\"))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Factor-labels-in-R.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61482\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Factor-labels-in-R.jpg\" alt=\"Factor labels in R\" width=\"1299\" height=\"743\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Factor-labels-in-R.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Factor-labels-in-R-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Factor-labels-in-R-300x172.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Factor-labels-in-R-768x439.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Factor-labels-in-R-1024x586.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Factor-labels-in-R-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>Finally, if you want to exclude any level from your factor, you can make use of the exclude argument. For example, let us exclude \u201cNorth\u201d from the list of levels. In order to do so, we initialise exclude with \u201cNorth\u201d.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; factor(directions, levels= c(\"North\", \"East\", \"South\", \"West\"), exclude = \"North\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-exculde-levels-in-factor-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55773\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-exculde-levels-in-factor-.png\" alt=\"Excluding level from Factors\" width=\"1305\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-exculde-levels-in-factor-.png 1305w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-exculde-levels-in-factor--150x85.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-exculde-levels-in-factor--300x170.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-exculde-levels-in-factor--768x436.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-exculde-levels-in-factor--1024x581.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/How-to-exculde-levels-in-factor--520x295.png 520w\" sizes=\"auto, (max-width: 1305px) 100vw, 1305px\" \/><\/a><\/p>\n<p><em><strong>Master all the <a href=\"https:\/\/data-flair.training\/blogs\/r-arguments-introduction\/\">Types of Arguments in R programming<\/a><\/strong><\/em><\/p>\n<h3>How to Generate Factor Level in R<\/h3>\n<p>In order to generate factor levels in <a href=\"https:\/\/www.r-project.org\/\">R<\/a>, we make use of the <em>gl()<\/em> function. The syntax for generating factor is gl(n, k, labels) where n is an integer specifying the number of levels.<\/p>\n<ul>\n<li>k is an integer that gives out a number of replications.<\/li>\n<li>And, labels are simply the vector of labels for our factor.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; #Author DataFlair\r\n&gt; BigData &lt;- gl(3, 2, labels = c(\"Hadoop\", \"Spark\",\"Flink\"))\r\n&gt; print(BigData)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Generate-levels-in-R-Factors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55780\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Generate-levels-in-R-Factors.png\" alt=\"Generating Factor Level \" width=\"1309\" height=\"745\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Generate-levels-in-R-Factors.png 1309w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Generate-levels-in-R-Factors-150x85.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Generate-levels-in-R-Factors-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Generate-levels-in-R-Factors-768x437.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Generate-levels-in-R-Factors-1024x583.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Generate-levels-in-R-Factors-520x296.png 520w\" sizes=\"auto, (max-width: 1309px) 100vw, 1309px\" \/><\/a><\/p>\n<h3>Accessing Components of Factor in R<\/h3>\n<p>Let us first create a factor \u201cdata\u201d as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#DataFlair\r\ncompass &lt;- c(\"East\",\"West\",\"East\",\"North\")\r\ndata &lt;- factor(compass)\r\ndata<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Accessing-Components-of-Factor-in-R.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55781\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Accessing-Components-of-Factor-in-R.png\" alt=\"Accessing Components of Factor\" width=\"1301\" height=\"740\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Accessing-Components-of-Factor-in-R.png 1301w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Accessing-Components-of-Factor-in-R-150x85.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Accessing-Components-of-Factor-in-R-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Accessing-Components-of-Factor-in-R-768x437.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Accessing-Components-of-Factor-in-R-1024x582.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Accessing-Components-of-Factor-in-R-520x296.png 520w\" sizes=\"auto, (max-width: 1301px) 100vw, 1301px\" \/><\/a><\/p>\n<p>There are various ways to access the elements of a factor in R. Some of the ways are as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; #Author DataFlair\r\n&gt; data[4] #Accessing the 4th element\r\n&gt; data[c(2,3)] #Accessing the 2nd &amp; 3rd element\r\n&gt; data[-1] #Accessing everything except 1st element\r\n&gt; data[c(TRUE, FALSE, TRUE, TRUE)] #Accessing using Logical Vector<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Ways-to-access-the-elements-in-factor-in-R.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-55782 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Ways-to-access-the-elements-in-factor-in-R.png\" alt=\"Access Elements in Factor\" width=\"1299\" height=\"743\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Ways-to-access-the-elements-in-factor-in-R.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Ways-to-access-the-elements-in-factor-in-R-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Ways-to-access-the-elements-in-factor-in-R-300x172.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Ways-to-access-the-elements-in-factor-in-R-768x439.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Ways-to-access-the-elements-in-factor-in-R-1024x586.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Ways-to-access-the-elements-in-factor-in-R-520x297.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Wait! Have you checked &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/r-data-frame\/\">R Data Frame Tutorial<\/a><\/strong><\/em><\/p>\n<h3>How to Modify an R Factor?<\/h3>\n<p>To modify a factor, we are only limited to the values that are not outside the predefined levels.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(data)\r\ndata[2] &lt;- \"North\" #Modifying 2nd element\r\ndata[3] &lt;- \"South\" #Cannot Modify Factor with an Element outside its scope<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Modify-an-R-Factor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55783\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Modify-an-R-Factor.png\" alt=\"Modify an R Factor\" width=\"1301\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Modify-an-R-Factor.png 1301w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Modify-an-R-Factor-150x85.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Modify-an-R-Factor-300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Modify-an-R-Factor-768x437.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Modify-an-R-Factor-1024x583.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/Modify-an-R-Factor-520x296.png 520w\" sizes=\"auto, (max-width: 1301px) 100vw, 1301px\" \/><\/a><\/p>\n<p>In the above example, modifying the third element with \u201cSouth\u201d gave out an error because it is not present in the pre-defined levels. In order to bypass this, we will have to add another level that includes \u201cSouth\u201d and modify our factor.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(data) \r\nlevels(data) &lt;- c(levels(data), \"South\") \r\ndata[3] &lt;- \"South\" \r\nprint(data)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/modify-another-level-in-R-factor-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55784\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/modify-another-level-in-R-factor-.png\" alt=\"modify another level in R factor\" width=\"1299\" height=\"740\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/modify-another-level-in-R-factor-.png 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/modify-another-level-in-R-factor--150x85.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/modify-another-level-in-R-factor--300x171.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/modify-another-level-in-R-factor--768x438.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/modify-another-level-in-R-factor--1024x583.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/modify-another-level-in-R-factor--520x296.png 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h2>Factor Functions in R<\/h2>\n<p><span style=\"font-weight: 400\">In this section, we will discuss some key functions that are related to factor. Some of these functions;\u00a0<em>is.factor(), as.factor(), is.ordered(), as.ordered(). is.factor()<\/em> checks if the input is present in the form of factor and returns a Boolean value (TRUE or FALSE). as.factor() takes the input (usually a vector) and converts it into a factor. is.ordered() checks if the factor is ordered and returns boolean TRUE or FALSE. The as.ordered() function takes an unordered function and returns a factor that is arranged in order. <\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">f_directions &lt;- factor(directions)\r\nis.factor(f_directions) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\nas.factor(directions) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\nis.ordered(f_directions) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\nas.ordered(f_directions) \u00a0\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Function.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55792\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Function.png\" alt=\"Factor Functions in R\" width=\"1301\" height=\"745\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Function.png 1301w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Function-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Function-300x172.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Function-768x440.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Function-1024x586.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Function-520x298.png 520w\" sizes=\"auto, (max-width: 1301px) 100vw, 1301px\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>Now, it&#8217;s easy for you to create and generate factors in R. Factors are the important part of Data Structures. To master in R language, you should have an entire of factors.<\/p>\n<p><em><strong>Now, it&#8217;s the right time to discuss <a href=\"https:\/\/data-flair.training\/blogs\/r-control-structures\/\">R Control Structures<\/a><\/strong><\/em><\/p>\n<p>If you have any suggestion or feedback, please post it in the comment section.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1282,&quot;href&quot;:&quot;https:\\\/\\\/www.r-project.org&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>R factor is used to store\u00a0categorical data as levels. It can store both character and integer types of data. These factors are created with the help of factor() functions, by taking a vector as&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":55786,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[11176,11188,11189],"class_list":["post-4444","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-r-data-types","tag-r-factor","tag-r-factor-functions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>R Factor - Learn the complete process from Creation to Modification! - DataFlair<\/title>\n<meta name=\"description\" content=\"With this tutorial, learn to create R factor, generate factor level, access factor components, modify factor and implement factor functions in R.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/r-factor-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Factor - Learn the complete process from Creation to Modification! - DataFlair\" \/>\n<meta property=\"og:description\" content=\"With this tutorial, learn to create R factor, generate factor level, access factor components, modify factor and implement factor functions in R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/r-factor-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-12T04:21:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T11:56:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R Factor - Learn the complete process from Creation to Modification! - DataFlair","description":"With this tutorial, learn to create R factor, generate factor level, access factor components, modify factor and implement factor functions in R.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/","og_locale":"en_US","og_type":"article","og_title":"R Factor - Learn the complete process from Creation to Modification! - DataFlair","og_description":"With this tutorial, learn to create R factor, generate factor level, access factor components, modify factor and implement factor functions in R.","og_url":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-10-12T04:21:26+00:00","article_modified_time":"2021-08-25T11:56:33+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"R Factor &#8211; Learn the complete process from Creation to Modification!","datePublished":"2017-10-12T04:21:26+00:00","dateModified":"2021-08-25T11:56:33+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/"},"wordCount":803,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg","keywords":["R Data Types","R Factor","R factor Functions"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/r-factor-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/","url":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/","name":"R Factor - Learn the complete process from Creation to Modification! - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg","datePublished":"2017-10-12T04:21:26+00:00","dateModified":"2021-08-25T11:56:33+00:00","description":"With this tutorial, learn to create R factor, generate factor level, access factor components, modify factor and implement factor functions in R.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/r-factor-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/10\/R-factor-Tutorial.jpg","width":802,"height":420,"caption":"Learn R Factor from Strach"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/r-factor-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"R Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/r\/"},{"@type":"ListItem","position":3,"name":"R Factor &#8211; Learn the complete process from Creation to Modification!"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=4444"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4444\/revisions"}],"predecessor-version":[{"id":61500,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4444\/revisions\/61500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/55786"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=4444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=4444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=4444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}