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?
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") .
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 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.
nlevels() function in R Language is used to get the number of levels of a factor.
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'")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With