Future-Proof Your Career, Master Data Skills + AI

Blog

Blog

# Factor Levels In R: Using Categorical & Ordinal Variables

by | 7:00 pm EST | March 09, 2022 | Power BI

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.

## Conclusion

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.

George

## Power BI Copilot: Enhancing Data Analysis with AI Integration

Are you ready to elevate your data analysis capabilities? Then let's delve into the realm of Power BI...

## What is Microsoft Fabric? A Guide to Features & Benefits

Imagine a one-stop shop that not only crunches numbers like it's eating cereal for breakfast but also...

## Python AttributeError: ‘dict’ object has no attribute Fix

Python is widely appreciated for its readability and ease of understanding. Among its numerous...

## Power BI vs Tableau: Differences Compared 2023

In the world of data visualization and analytics, two industry-leading data visualization tools stand...

## Pandas Drop Index Column: Explained With Examples

Pandas is a widely used Python library for data manipulation and analysis. One essential functionality...

## Microsoft DP-500 Explained (Bonus Cheat Sheet)

Data is the backbone of businesses these days, and having proof that you know how to handle and make...

## Power BI Financial Dashboard Examples: Let’s Get Specific

Power BI financial dashboards provide a quick and easy way to monitor an organization's financial...

## Power BI Themes: User Guide With Examples

Power BI is a powerful business analytics tool that helps you visualize and analyze data from various...

## Power BI Hierarchy: User Guide With Examples

Imagine being able to look at your data from every which way — from the bird's eye view right down to...

## Power BI Calendar Table: What Is it And How to Use It

Time is of the essence in the world of data analysis, and without a proper understanding of when or how...

## How to Sort Alphabetically in Excel with Multiple Columns

Ever find yourself staring at a heap of info in Excel, scratching your head about how to make it all...