{"id":75429,"date":"2020-01-13T15:52:45","date_gmt":"2020-01-13T10:22:45","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75429"},"modified":"2020-01-13T15:52:45","modified_gmt":"2020-01-13T10:22:45","slug":"r-factors","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/r-factors\/","title":{"rendered":"R Factors &#8211; Operating on Factors and Factor Levels"},"content":{"rendered":"<p>Factors are data structures in R that <strong>store<\/strong> <strong>categorical<\/strong><strong>\u00a0data<\/strong>. In datasets, there are often fields that take only a few predefined values. For example &#8211; gender, availability, country, marital status, etc. Such data is called categorical data.<\/p>\n<p>R factors only allow values that are among a predefined set of possible values known as <strong>levels<\/strong>.<\/p>\n<p>Today, we are going to learn everything about factors in the R programming language.<\/p>\n<p>Get ready for an exciting tutorial, because here it comes!<\/p>\n<h3>Categorical vs. Continuous Variables<\/h3>\n<p>In a dataset, there are two types of variables:<\/p>\n<ol>\n<li>Continuous variables<\/li>\n<li>Categorical variables<\/li>\n<\/ol>\n<p><strong>Continuous variables<\/strong> can take any values. They can be numerics, integers or characters. Examples of such variables would be names, addresses, phone numbers, etc..<\/p>\n<p><strong>Categorical variables<\/strong> can only take values from a finite group of possible values. They can be logical, characters, numerics or integers. Examples of categorical variables would be gender, marital status, logical values, country, etc.<\/p>\n<h2>Factors in R<\/h2>\n<p>Factors are data structures in R that store categorical data. They have a <strong>levels<\/strong> attribute that holds all the possible values that elements of the factor can take.<\/p>\n<p>R factors can be of any type. They only allow values permitted by the levels. Factors can have NA values, if a value that is not in the levels of a factor is entered into it.<\/p>\n<h3>Creating a Factor<\/h3>\n<p>We use the <strong><code>factor()<\/code><\/strong> function to create factors. The following is the syntax of the <strong><code>factor()<\/code><\/strong> function:<\/p>\n<p><span style=\"color: #333333\"><strong><code>factor_name=factor(x=character(),levels,labels,exclude,ordered,nmax)<\/code><\/strong><\/span><\/p>\n<p>Where<strong><code> x<\/code><\/strong> is a vector with the data for the factor,<\/p>\n<p><strong><code>levels<\/code><\/strong>is an optional vector with unique values that x might take,<\/p>\n<p><strong><code>labels<\/code><\/strong> is an optional vector of labels for the levels in the factor,<\/p>\n<p><strong><code>exclude<\/code><\/strong> is a set of values that are excluded from the levels of the factor,<\/p>\n<p><strong><code>ordered<\/code><\/strong> is a logical value that determines whether the factor is an ordered or unordered factor,<\/p>\n<p><strong><code>nmax<\/code><\/strong> is an upper limit on the number of levels.<\/p>\n<p><strong>Code<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; gender &lt;- c(\"male\",\"male\",\"female\",\"male\",\"female\",\"male\")\n&gt; fac1 &lt;- factor(gender)\n&gt; fac1<\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/facors-in-r-creating-a-factor-factor-fac1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75437\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/facors-in-r-creating-a-factor-factor-fac1.png\" alt=\"creating R factor factor() fac1\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>Indexing R Factor<\/h3>\n<p>We can use the same indexing techniques as a vector to access the elements of a factor.<\/p>\n<p>Forgot the basic data structure in R? <strong>Revis<\/strong><strong>e <\/strong><em><strong>the <a href=\"https:\/\/techvidvan.com\/tutorials\/r-vector\/\">concept of R vector<\/a><\/strong><\/em>.<\/p>\n<h4>1. Using positive integers<\/h4>\n<p>We can index factors by using positive integers or vectors of positive integers.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1[3]<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1[c(2:4)]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-indexing-factors-positive-integers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75438\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-indexing-factors-positive-integers.png\" alt=\"indexing R factors positive integers\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>2. Using negative integers<\/h4>\n<p>We can use negative integers or vectors of negative integers to exclude certain elements from R factor.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1[-2]<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1[c(-3,-4)]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-indexing-factors-negative-integers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75439\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-indexing-factors-negative-integers.png\" alt=\"indexing R factors negative integers\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h4>3. Using logical vectors<\/h4>\n<p>We can index R factors by using logical vectors.<\/p>\n<p><strong>Code: <\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1[c(T,F,T,T,F,F)]<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1[c(T,F,T)] #logical index vector recycling<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-indexing-with-logical-vectors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75440\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-indexing-with-logical-vectors.png\" alt=\"indexing with logical vectors - R factors\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>Modifying the R Factor<\/h3>\n<p>We can modify existing values and add new ones as well by using <strong>reassignment<\/strong>. The new values must be within the level of the factor.<\/p>\n<p><strong>Initial state of factor:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] male male female male female male<br \/>\nLevels: female male<\/div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1[2] &lt;- \"female\"\n&gt; fac1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] male female female male female male<br \/>\nLevels: female male<\/div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1[7] &lt;- \"other\"<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"color: #ff0000\">Warning message:<\/span><br \/>\n<span style=\"color: #ff0000\">In `[&lt;-.factor`(`*tmp*`, 7, value = &#8220;other&#8221;) :<\/span><br \/>\n<span style=\"color: #ff0000\">invalid factor level, NA generated<\/span><\/div>\n<p><strong>Note<\/strong>: a factor does not allow values that are not in its level.<\/p>\n<p><strong>Final state of factor:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; fac1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] male female female male female male &lt;NA&gt;<br \/>\nLevels: female male<\/div>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-modifying-a-factor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75441\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-modifying-a-factor.png\" alt=\"modifying R factor\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>Adding and Dropping Levels of R Factor<\/h3>\n<p>To add a value that does not exist in the level of a factor, we must add it to the level first.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; levels(fac1) &lt;- c(\"female\",\"male\",\"other\")\n&gt; fac1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-adding-a-level-to-factor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75442\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-adding-a-level-to-factor.png\" alt=\"adding a level to r factor\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p>We can also remove levels from a factor by using the <strong><code>droplevels()<\/code><\/strong> function. This function removes unused levels from a factor.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; droplevels(fac1)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-dropping-a-level-from-factor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75443\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-dropping-a-level-from-factor.png\" alt=\"dropping a level from R factor\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>Ordered Factors in R<\/h3>\n<p>We can classify R factors as <strong>ordered or unordered<\/strong>. By default, the levels are arranged in alphabetical order and are all considered equal irrespective of their arrangement. For comparison purposes, these levels can be ordered according to increasing weight or value.<\/p>\n<p>Here is an example of an <strong>unordered<\/strong> R factor:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; sizes &lt;- c(\"s\",\"m\",\"s\",\"l\",\"m\",\"xs\",\"l\",\"m\",\"xl\",\"xxl\",\"s\", \"l\",\"xs\",\"xl\",\"m\",\"l\")\n&gt; sizef &lt;- factor(sizes)\n&gt; sizef<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-unordered-factor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75444\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-unordered-factor.png\" alt=\"unordered R factor\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p>We can provide a specific order of levels by using the <strong><code>levels =<\/code><\/strong> argument of the <strong><code>factor()<\/code><\/strong> function. However, the resultant factor is still considered unordered.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; sizef &lt;- factor(sizes,levels=c(\"xs\",\"s\",\"m\",\"l\",\"xl\",\"xxl\"))\n&gt; sizef<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-ordered-factors-providing-levels.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75445\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-ordered-factors-providing-levels.png\" alt=\"ordered R factors providing levels\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p>We can also use the <strong><code>ordered()<\/code><\/strong> function to create an ordered factor. This function has the same syntax as the <strong><code>factor()<\/code><\/strong> function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; sizeof &lt;- ordered(sizes,levels=c(\"xs\",\"s\",\"m\",\"l\",\"xl\",\"xxl\"))\n&gt; sizeof<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-ordered-factors-using-ordered.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75446\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-ordered-factors-using-ordered.png\" alt=\"ordered R factors using ordered()\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p>We can also convert unordered factors into ordered ones by using the <strong><code>as.ordered()<\/code><\/strong> function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; as.ordered(sizef)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-converting-factors-with-as.ordered.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75447\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-converting-factors-with-as.ordered.png\" alt=\"converting R factors with as.ordered()\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>Functions of R Factors<\/h3>\n<p>There are a few functions that give us information about the R factor variables we use. Functions like <strong><code>is.factor()<\/code><\/strong>, <strong><code>as.factor()<\/code><\/strong>, <strong><code>is.ordered()<\/code><\/strong>, etc.. This is what these functions do:<\/p>\n<h4><strong>1. <code>is.factor()<\/code><\/strong><\/h4>\n<p>The <strong><code>is.factor()<\/code><\/strong> function checks if a variable is a factor or not. It returns a logical value of <strong><code>TRUE<\/code><\/strong> if the variable is a factor and <strong><code>FALSE<\/code><\/strong> if it is not.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; is.factor(fac1)\n&gt; is.factor(gender)<\/pre>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-factor-functions-is.factor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75460\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-factor-functions-is.factor.png\" alt=\"R factor functions is.factor()\" width=\"1299\" height=\"740\" \/><\/a><\/p>\n<h4><strong>2. <code>as.factor()<\/code><\/strong><\/h4>\n<p>The <strong><code>as.factor()<\/code><\/strong> function converts vector inputs into factors.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; as.factor(c(TRUE,FALSE,TRUE,TRUE,FALSE))<\/pre>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-factor-functions-as.factor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75461\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-factor-functions-as.factor.png\" alt=\"R factor functions as.factor()\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4><strong>3. <code>is.ordered()<\/code><\/strong><\/h4>\n<p>The <strong><code>is.ordered()<\/code><\/strong> function checks if the variable is an ordered factor or not. This function returns <strong><code>TRUE<\/code><\/strong> if the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Variable_(computer_science)\">variable<\/a> is an ordered factor and <strong><code>FALSE<\/code><\/strong> if it is not.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; is.ordered(sizeof)\n&gt; is.ordered(fac1)<\/pre>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-factor-functions-is.ordered.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75462\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/factors-in-r-factor-functions-is.ordered.png\" alt=\"R factor functions is.ordered()\" width=\"1299\" height=\"740\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>R factors are data structures that store categorical data. They only allow observations of certain predefined values. Therefore, they are useful for fields that have a limited number of possible values like gender, marital status, availability, confirmation, etc..<\/p>\n<p>In this article, we learned about R factors. We learned how to create them and how to modify them. We saw what they are used for. Finally, we looked at a few functions that provide more insights into factors and convert other variables into R factors.<\/p>\n<p>Before start working with R you must have a basic understanding of <em><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/r-data-structures\/\">data structures<\/a><\/strong><\/em> in R.<\/p>\n<p>Still, have some doubts about the R factor? Ask us and our <strong>TechVidvan<\/strong> experts will be happy to help you.<\/p>\n<p>Keep Learning ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Factors are data structures in R that store categorical\u00a0data. In datasets, there are often fields that take only a few predefined values. For example &#8211; gender, availability, country, marital status, etc. Such data is&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75472,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[1360,1361,1362,1363,1364,1365,1366,1367],"class_list":["post-75429","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-droplevels-r","tag-factor-function-in-r","tag-factors-in-r-programming","tag-r-factor-functions","tag-r-factor-levels","tag-r-factor-levels-order","tag-r-factors","tag-r-ordered-factor"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>R Factors - Operating on Factors and Factor Levels - TechVidvan<\/title>\n<meta name=\"description\" content=\"What is Factor in R? Factors are data structures in R that store only a few predefined values. In this article, you will learn to work with factors.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/r-factors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Factors - Operating on Factors and Factor Levels - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"What is Factor in R? Factors are data structures in R that store only a few predefined values. In this article, you will learn to work with factors.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/r-factors\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-13T10:22:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/factors-in-R.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=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R Factors - Operating on Factors and Factor Levels - TechVidvan","description":"What is Factor in R? Factors are data structures in R that store only a few predefined values. In this article, you will learn to work with factors.","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:\/\/techvidvan.com\/tutorials\/r-factors\/","og_locale":"en_US","og_type":"article","og_title":"R Factors - Operating on Factors and Factor Levels - TechVidvan","og_description":"What is Factor in R? Factors are data structures in R that store only a few predefined values. In this article, you will learn to work with factors.","og_url":"https:\/\/techvidvan.com\/tutorials\/r-factors\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-13T10:22:45+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/factors-in-R.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"R Factors &#8211; Operating on Factors and Factor Levels","datePublished":"2020-01-13T10:22:45+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/"},"wordCount":898,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/factors-in-R.jpg","keywords":["droplevels R","factor function in R","factors in R programming","R factor Functions","r factor levels","R factor levels order","R factors","R ordered factor"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/r-factors\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/","url":"https:\/\/techvidvan.com\/tutorials\/r-factors\/","name":"R Factors - Operating on Factors and Factor Levels - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/factors-in-R.jpg","datePublished":"2020-01-13T10:22:45+00:00","description":"What is Factor in R? Factors are data structures in R that store only a few predefined values. In this article, you will learn to work with factors.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/r-factors\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/factors-in-R.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/factors-in-R.jpg","width":802,"height":420,"caption":"R factors"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/r-factors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"R Factors &#8211; Operating on Factors and Factor Levels"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75429"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75429\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75472"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}