Search code examples
rvariablesr-factor

How to remove ordering of the levels from factor variable in R?


The title says it all, I ordered a factor variable when I generated it, now I would like to remove the ordering and use it as an unordered factor variable. And another question, if I use my factor variable as a predictor in a regression does it make a difference to R if it is ordered (ordinal) or simple factor variable (categorical)?


Solution

  • All you need is

    x <- factor( x , ordered = FALSE )
    

    e.g.

    x <- factor( c(1,2,"a") , ordered = TRUE )
    x
    #[1] 1 2 a
    #Levels: 1 < 2 < a
    
    x <- factor( x , ordered = FALSE )
    x
    #[1] 1 2 a
    #Levels: 1 2 a