Level Ordering of Factors in R Programming
Last Updated :
19 Apr, 2025
Level ordering controls how categorical values are stored, displayed, and interpreted in analyses and plots. By default, R orders factor levels alphabetically. In this article, we will see the level ordering of factors in the R Programming Language.
What Are Factors in R?
Factors are data objects used to categorize data and store it as levels. They can store a string as well as an integer. They represent columns as they have a limited number of unique values. Factors in R can be created using the factor() function. It takes a vector as input. c() function is used to create a vector with explicitly provided values.
Example:
In this example, x is a vector with 8 elements. To convert it to a factor the function factor() is used. Here, there are 8 factors and 3 levels. Levels are the unique elements in the data. It can be found using the levels() function
R
x <- c("Pen", "Pencil", "Brush", "Pen",
"Brush", "Brush", "Pencil", "Pencil")
print(x)
print(is.factor(x))
# Apply the factor function.
factor_x = factor(x)
levels(factor_x)
Output :
[1] "Pen" "Pencil" "Brush" "Pen" "Brush" "Brush" "Pencil" "Pencil"
[1] FALSE
[1] "Brush" "Pen" "Pencil"
Ordering Factor Levels
Ordered factors levels are an extension of factors. It arranges the levels in increasing order. We use two functions: factor() and argument ordered().
1. Using the factor() Function
You can rearrange factor levels by actually stating the order in the levels argument to the factor() function. This is perfect if you have an established, predetermined order for your factor levels.
Syntax:
factor(data, levels =c(""), ordered =TRUE)
Parameter:
- data: input vector with explicitly defined values.
- levels(): Mention the list of levels in c function.
- ordered: It is set true for enabling ordering.
Example: In this example, the size vector is created using the c function. Then it is converted to a factor. For the ordering factor, the factor() function is used along with the arguments described as "Orderd=TRUE" . Thus the sizes are arranged in order.
R
size = c("small", "large", "large", "small",
"medium", "large", "medium", "medium")
size_factor <- factor(size)
print(size_factor)
ordered.size <- factor(size, levels = c(
"small", "medium", "large"), ordered = TRUE)
print(ordered.size)
Output:
[1] small large large small medium large medium medium
Levels: large medium small
[1] small large large small medium large medium medium
Levels: small < medium < large
2. Using the ordered() Function
The same can be done using the ordered function.
Example:
R
sizes <- factor(c("small", "large", "large",
"small", "medium"))
# ordering the levels
sizes <- ordered(sizes, levels = c("small", "medium", "large"))
print(sizes)
Output:
[1] small large large small medium
Levels: small < medium < large
Level ordering visualization in R
To visualise the level ordering , we have a dataset of student grades, and we want to create a boxplot to compare the distribution of grades for different class levels (freshman, sophomore, junior, and senior). We can create a factor variable to represent the class levels and specify the level ordering so that the boxplot is ordered by class level.
Example:
In this example, we create a sample dataset of student grades with a grade column and a level column representing the class level of each student. We then create a factor variable level from the level column and specify the level ordering as "freshman", "sophomore", "junior", and "senior" using the factor() function.
Finally, we create a boxplot of grades by class level using the boxplot() function. The grade column represents the response variable, and the level column represents the explanatory variable. We also specify a title for the plot.
R
grades <- data.frame(
grade = c(75, 82, 68, 92, 89, 78, 85, 90, 72, 81, 94, 87, 79, 86, 91),
level = factor(c(rep("freshman", 5), rep("sophomore", 4), rep("junior", 3), rep("senior", 3)))
)
grades$level <- factor(grades$level, levels = c("freshman", "sophomore", "junior", "senior"))
boxplot(grade ~ level, data = grades, main = "Student Grades by Class Level")
Output:
Level Ordering of Factors in R Programming
Similar Reads
Get or Set Levels of a Factor in R Programming - levels() Function levels() function in R Language is used to get or set the levels of a factor. Syntax: levels(x) Parameters: x: Factor Object Example 1: Python3 1== # R program to get levels of a factor # Creating a factor gender <- factor(c("female", "male", "male", "female
1 min read
How to Code in R programming? R is a powerful programming language and environment for statistical computing and graphics. Whether you're a data scientist, statistician, researcher, or enthusiast, learning R programming opens up a world of possibilities for data analysis, visualization, and modeling. This comprehensive guide aim
4 min read
How to Rename Factor Levels in R? In this article, we are going to how to rename factor levels in R programming language. A factor variable in R is represented using categorical variables which are represented using various levels. Each unique value is represented using a unique level value. A factor variable or vector in R can be d
4 min read
Forcats Package in R Programming 'forcats' is a package in R Programming that provides tools for working with categorical (factor) variables. Categorical variables are variables that take on a limited, discrete set of values or levels, and are used to represent qualitative or nominal data. Some examples of categorical variables inc
9 min read
Factor Analysis in R programming Factor Analysis (FA) is a statistical method that is used to analyze the underlying structure of a set of variables. It is a data reduction technique that attempts to account for the intercorrelations among a large number of variables in terms of fewer unobservable (latent) variables, or factors. In
6 min read