Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename one level of a factor in R

Tags:

I'm attempting to rename the level A of factor column1 in the dataframe df in R. My current approach is this:

levels(df[!is.na(df$column1) & df$column1 == 'A',]) <- 'B' 

which doesn't throw any errors or warnings but is completely ineffective.

B is not an already existing level (which from trial and error I came to suspect was important), so the following, my first attempt, didn't work either

df[!is.na(df$column1) & df$column1 == 'A', 'column1'] <- 'B' 

Could anyone guide me to the correct approach?

like image 830
userManyNumbers Avatar asked Apr 29 '15 00:04

userManyNumbers


People also ask

How do you change the factor level names 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 remove a specific level of 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.

What does factor () do in R?

What is Factor in R? Factor in R is a variable used to categorize and store the data, having a limited number of different values. It stores the data as a vector of integer values. Factor in R is also known as a categorical variable that stores both string and integer data values as levels.

How do I assign a number to a level in R?

nlevels() function in R Language is used to get the number of levels of a factor.


1 Answers

I was going to suggest

levels(df$column1)[levels(df$column1)=="A"] <- "B" 

or use the utility function plyr::revalue:

library("plyr") df <- transform(df,           column1=revalue(column1,c("A"="B"))) 

transform() is a little sugar that's not necessary; you could use df$column1 <- revalue(df$column1(...))

For completeness, car::recode also works, although I find it a little bit clunkier that plyr::revalue (because the recoding is specified as a quoted string).

car::recode(df$column1,"'A'='B'") 
like image 74
Ben Bolker Avatar answered Oct 04 '22 03:10

Ben Bolker