Rename one level of a factor in R
Solution 1
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'")
Solution 2
One way would be just to change the label of the level. First, some test data
df <- data.frame(column1=c("A","B","C","A","B"))
and now we replace "A" with "X"
levels(df$column1) <- gsub("A","X", levels(df$column1))
and we can see that it's changed
column1
1 X
2 B
3 C
4 X
5 B
You might need to be careful with gsub()
since it accepts a regular expression. A more specific replacement would be
gsub("^A$","X", levels(df$column1))
to match exactly "A" and not "CAB" or something else with a capital A.
userManyNumbers
Updated on June 10, 2022Comments
-
userManyNumbers about 2 years
I'm attempting to rename the level
A
of factorcolumn1
in the dataframedf
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 eitherdf[!is.na(df$column1) & df$column1 == 'A', 'column1'] <- 'B'
Could anyone guide me to the correct approach?