Ordering Factor Values in R – Brief Guide

Filed Under: R Programming
Ordering Of Factors In R

Hello, readers! In this article, we will be focusing on ordering factor values in R. Within which, we would be discussing some important functions regarding ordering.

So, let us begin!


What are Factors in R?

Factors are a data object in R that enables us to store factor type values. The variables that happen to possess categories in their variable pattern.

With factors, we can store the categorical variables and inculcate the groups within them as a separate level in factors.

Ordered factors actually possess ordered levels/groups of the categories in a Factor. They have a sense of coherence in the order of their levels, let’s say, {high, medium, small}.

Unordered factors do not possess such orders in the levels of the factors.

Let us now focus on the creation of orders for factors in the upcoming section.


1. Ordering factor values using as.ordered() function in R

At first, we would want to elaborate a bit about ordered factors. Ordering a factor type variable actually means ordering or sorting the levels of the factor type data in a particular order/manner.

With as.ordered() function, we can manipulate the position of the factors in a sorted manner. That is, we can align an unordered factor in an ordered manner.

Example 01: Ordering factor value of character type

In this example, we have created factor type data using factor() function. Further, we have aligned the data into an order using as.ordered() function.

rm(list = ls())
 
library(dplyr) 

dta <- as.factor(c("y", "z", "a")) 

print("Unordered Factors:") 
print(dta) 

print("Factor after ordering:") 
print(as.ordered(dta)) 

Output:

> print("Unordered Factors:") 
[1] "Unordered Factors:"
> print(dta) 
[1] y z a
Levels: a y z

> print("Factor after ordering:") 
[1] "Factor after ordering:"
> print(as.ordered(dta)) 
[1] y z a
Levels: a < y < z

Example 02: Ordering factor values of numeric type in R

In this example, we have created factors of numeric type levels. Further, we have ordered the same using the as.ordered() function.

rm(list = ls())
 
library(dplyr) 

dta <- as.factor(c(10,5,3,30,2)) 

print("Unordered Factors:") 
print(dta) 

print("Factor after ordering:") 
print(as.ordered(dta)) 

Output:

> print("Unordered Factors:") 
[1] "Unordered Factors:"
> print(dta) 
[1] 10 5  3  30 2 
Levels: 2 3 5 10 30

> print("Factor after ordering:") 
[1] "Factor after ordering:"
> print(as.ordered(dta)) 
[1] 10 5  3  30 2 
Levels: 2 < 3 < 5 < 10 < 30

Check if the Factor is ordered?

Having said about Ordering Factors, a question arises to my mind..

How do we check if the Factor is ordered or not?

This is when R is.ordered() function comes into picture. The is.ordered() function is a boolean function that checks if the factor variable is ordered or not. That is, it returns True if the variables are well ordered, else it returns False.

Example 01:

In this example, we have created a factor of numeric type levels using factor() function.

Further, upon applying is.ordered() function, we get to know that the factor is not ordered. Which is obvious because we have not used as.ordered() function here to order the factor data values.

Thus, it returns False.

rm(list = ls())
 
library(dplyr) 

dta <- as.factor(c(10,5,3,30,2)) 

print("Unordered Factors:") 
print(dta) 

print(is.ordered(dta))

Output:

> print("Unordered Factors:") 
[1] "Unordered Factors:"
> print(dta) 
[1] 10 5  3  30 2 
Levels: 2 3 5 10 30

> print(is.ordered(dta))
[1] FALSE

Example 02;

Now, in this example, after creating the factor values, firstly, we have ordered them using as.ordered() function. Further, we have checked if the factor is ordered or not using is.ordered() function.

And, the function returns True because we had ordered them in the previous step.

rm(list = ls())
 
library(dplyr) 

dta <- as.factor(c(5,4,3)) 

print("Unordered Factors:") 
dta = ordered(dta,levels = c(5,4,3))
print(ordered(dta)) 

print(is.ordered(dta))

Output:

[1] "Unordered Factors:"
> dta = ordered(dta,levels = c(5,4,3))
> print(ordered(dta)) 
[1] 5 4 3
Levels: 5 < 4 < 3
> 
> print(is.ordered(dta))
[1] TRUE

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned with us!

Till then, Happy Learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content