Convert & Create Ordered Factor in R (3 Examples) | ordered() & is.ordered() Functions
In this post, I’ll show how to create ordered factors using the ordered() function in R programming.
The page looks as follows:
Let’s dig in.
Example 1: Convert Vector to Ordered Factor Using ordered() Function
In this example, I’ll illustrate how to turn a factor vector into an ordered factor.
First, we have to create an example factor vector in R:
my_fac <- factor(c("c", "a", "b", "c", "a"), # Create factor levels = c("c", "a", "b")) my_fac # Print factor # [1] c a b c a # Levels: c a b
As you can see, our factor contains five elements and three factor levels.
Let’s check the class of our data object:
class(my_fac) # Check class of factor # [1] "factor"
At this point, our data object is a factor.
We can now convert our factor into an ordered factor using the ordered function as shown below:
my_fac_ordered1 <- ordered(my_fac) # Apply ordered function my_fac_ordered1 # Print ordered factor # [1] c a b c a # Levels: c < a < b
The output is already slightly different (note the angle brackets between the levels).
Let’s check the data type of our updated data object:
class(my_fac_ordered1) # Check class of ordered factor # [1] "ordered" "factor"
As you can see, we have converted our factor vector into an ordered factor vector.
Note that we could apply the as.ordered function to obtain the same result.
Example 2: Create Ordered Factor Using factor() Function & ordered Argument
The R syntax below demonstrates how to specify that a data object should be an ordered factor during the creation process of this data object.
For this, we have to specify the ordered argument within the factor function to be equal to TRUE:
my_fac_ordered2 <- factor(c("c", "a", "b", "c", "a"), # Create ordered factor levels = c("c", "a", "b"), ordered = TRUE) my_fac_ordered2 # Print ordered factor # [1] c a b c a # Levels: c < a < b
Let’s check the class of our data object:
class(my_fac_ordered2) # Check class of ordered factor # [1] "ordered" "factor"
It’s an ordered factor!
Example 3: Check for Ordered Factor Using is.ordered() Function
This example illustrates how to use the is.ordered function to test whether a data object is an ordered factor.
Let’s first apply the is.ordered function to the factor object that we have created at the beginning of Example 1:
is.ordered(my_fac) # Apply is.ordered function to factor # [1] FALSE
The RStudio console returns the logical indicator FALSE, i.e. the data object my_fac is not an ordered factor.
Let’s apply the is.ordered function to the data object that we have constructed in Example 2:
is.ordered(my_fac_ordered2) # Apply is.ordered function to ordered factor # [1] TRUE
This time, the is.ordered function prints the logical indicator TRUE, i.e. the data object my_fac_ordered2 is an ordered factor.
Video, Further Resources & Summary
I have recently released a video on my YouTube channel, which explains the R syntax of this article. You can find the video instruction below:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you could have a look at the related articles on my website. I have released several other tutorials that are related to the application of the ordered() function already:
- Convert Factor to Character Class
- Convert Factor to Dummy Indicator Variables for Every Level
- Convert Factor to Date in R
- Convert Character to Factor in R
- Introduction to R
You have learned in this tutorial how to apply the ordered() function and how to work with and handle ordered factors in the R programming language. In case you have additional questions, tell me about it in the comments section.
Statistics Globe Newsletter