How to convert unordered factor to ordered factor in R?

r
factor_analysis

#1

I am studying about factors in R, while studying it I have created a vector then used the factor function to convert categorical variable to factor variable.

speed_vector <- c("Fast", "Slow", "Slow", "Fast", "Ultra-fast") 
 factor_speed_vector <- factor(speed_vector)
factor_speed_vector
 Fast       Slow       Slow       Fast       Ultra-fast
Levels: Fast Slow Ultra-fast

As in the vector we can compare the term but after converting into the factors, it does not give information about the comparison of the term.I want to know how I can create the factors by which the terms in the categorical variable is arranged in ordered way.


#2

@sid100158 - You have to use ordered argument of the factor function and you also need to specify the order in which you want to arrange the levels.

speed_vector <- c("Fast", "Slow", "Slow", "Fast", "Ultra-fast")
factor_speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("Slow", "Fast", "Ultra-fast"))
factor_speed_vector
 Fast       Slow       Slow       Fast       Ultra-fast
Levels: Slow < Fast < Ultra-fast 

Hope this helps!

Regards,
Hinduja