Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 837
Pulse Avatar asked Jul 11 '13 11:07

Pulse


People also ask

How do you remove a level from a factor in R?

The droplevels() function in R can be used to drop unused factor levels. This function is particularly useful if we want to drop factor levels that are no longer used due to subsetting a vector or a data frame. where x is an object from which to drop unused factor levels.

How do you change the level of a factor in R?

How do I Rename Factor Levels in R? The simplest way to rename multiple factor levels is to use the levels() function. For example, to recode the factor levels “A”, “B”, and “C” you can use the following code: levels(your_df$Category1) <- c("Factor 1", "Factor 2", "Factor 3") .

How do I order a factor column in R?

To sort a numerical factor column in an R data frame, we would need to column with as. character then as. numeric function and then order function will be used.

How do you find the factor order in R?

Check if a Factor is an Ordered Factor in R Programming – is. ordered() Function. is. ordered() function in R Programming Language is used to check if the passed factor is an ordered factor.


1 Answers

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
like image 186
Simon O'Hanlon Avatar answered Oct 22 '22 09:10

Simon O'Hanlon