I would like combine level "A","B" into "A+B". I successfully did this by the following:
x <- factor(c("A","B","A","C","D","E","A","E","C"))
x
#[1] A B A C D E A E C
#Levels: A B C D E
l <- c("A+B","A+B","C","D+E","D+E")
factor(l[as.numeric(x)])
#[1] A+B A+B A+B C D+E D+E A+B D+E C
#Levels: A+B C D+E
Is there any more trivial way to do this? (i.e. more explainable function name such as combine.factor(f, old.levels, new.levels) would help to understand the code easier.)
Also, I try to find a well named function which probably work with data frame in dplyr package but no luck. The closest implementation is
df %>% mutate(x = factor(l[as.numeric(x)]))