Rename one level of a factor in R -
i'm attempting rename level a of factor column1 in dataframe df in r. current approach this:
levels(df[!is.na(df$column1) & df$column1 == 'a',]) <- 'b' which doesn't throw errors or warnings ineffective.
b not existing level (which trial , error came suspect important), following, first attempt, didn't work either
df[!is.na(df$column1) & df$column1 == 'a', 'column1'] <- 'b' could guide me correct approach?
i going suggest
levels(df$column1)[levels(df$column1)=="a"] <- "b" or use utility function plyr::revalue:
library("plyr") df <- transform(df, column1=revalue(column1,c("a"="b"))) transform() little sugar that's not necessary; use df$column1 <- revalue(df$column1(...))
for completeness, car::recode works, although find little bit clunkier plyr::revalue (because recoding specified quoted string).
car::recode(df$column1,"'a'='b'")
Comments
Post a Comment