Open In App

Level Ordering of Factors in R Programming

Last Updated : 19 Apr, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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:

boxplot
Level Ordering of Factors in R Programming

Next Article
Article Tags :
Practice Tags :

Similar Reads