This tutorial will go through factors and factor levels in R. You’ll learn how to create a factor and how to adjust factor levels.
Factors are used to store and work with variables in R.
In this tutorial, you’ll be dealing with categorical and ordinal variables. Categorical variables are variables that involve one or more categories that aren’t ordered in any specific way. An example would be colors. Ordinal variables, on the other hand, are similar to categorical variables with the difference that ordinal variables have clear ordering of the categories. This could be like low, medium, and high.
This is an introduction to more statistical terms. You are now slowly exploring R’s capabilities for data and statistical analysis.
Categorical Factor Levels In R
If you recall in another lesson about data frames, you used the dollar sign ($) to print out the Species column from the iris dataset. Do this again in RStudio. At the bottom-most part, there’s a line containing Levels composed of setosa, versicolor, and virginica.
This is R’s way of handling categories in data.
If you use the unique ( ) function, R will list out the unique values in the specified column. For example, if you Run unique (iris$Species), the Console displays the three Species level of iris.
There’s no inherent ordering for these levels. You can’t say that setosa is greater than the other two color categories. R, by default, arranges them into alphabetical order.
Ordinal Factor Levels In R
Now let’s try and explore factors with inherent ordering of the category.
Create a vector and name it orders. For this example, assign that vector with data using Starbucks’ cup size names: tall, venti, and grande. Then, print it out.
These should be arranged from smallest to the biggest; it should be tall, venti, and grande. But when you Run the unique ( ) function for orders, they aren’t arranged in that order.
Here’s how to turn them into ordinal variables. First, you need to create a new vector. In this case, the vector is called new_orders_factor. Assign this vector with the factor ( ) function. Inside this function, input the vector you want to set levels with. Then, indicate levels in the order you want them to appear.
Highlight this entire line of code and then Run it. A new Value is then added in Environment.
To check if a vector has been properly assigned as a factor, use the is.factor ( ) function. If you check the two vectors, orders and new_orders_factor, you can see that the former returns FALSE while the new vector is indeed a factor.
A factor is a special way to store a series of texts. And though it’s a character vector, it can be stored in a way that allows it to have a given number of categories that have a specific ordering of values or levels.
If you check using the levels ( ) function, you can see that the levels are now in the correct order.
Though this lesson may seem esoteric, you’ll see how this makes a difference when dealing with more advanced R coding. It’s important to learn about factors and levels since they often come up in many R coding and statistical analysis.