Convert an Unordered Factor to an Ordered Factor in R Programming – as.ordered() Function

as.ordered() function in R Language takes an unordered factor as argument and converts it into an ordered factor.

Syntax: as.ordered(factor)

Parameters:
factor: Unordered Factor to be converted

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a vector 
x<-c("North", "North", "East", "West"
  
# Converting vector into factor
Directions <- factor(x)
  
# Using as.ordered() Function
# to order an unordered factor
as.ordered(Directions)

chevron_right


Output:

[1] North North East  West 
Levels: East < North < West

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# creating vector size 
size = c("small", "large", "large", "small",  
         "medium", "large", "medium", "medium")   
sizes <- ordered(c("small", "large", "large"
                   "small", "medium")) 
    
# Using as.ordered() Function
# to order an unordered factor
as.ordered(sizes)

chevron_right


Output:

[1] small  large  large  small  medium
Levels: large < medium < small
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.