alberthkcheng - 3 months ago 32

R Question

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

Also, I try to find a well named function which probably work with data frame in

`df %>% mutate(x = factor(l[as.numeric(x)]))`

Answer

One option is `recode`

from `car`

```
library(car)
recode(x, "c('A', 'B')='A+B';c('D', 'E') = 'D+E'")
#[1] A+B A+B A+B C D+E D+E A+B D+E C
#Levels: A+B C D+E
```

It should also work with `dplyr`

```
library(dplyr)
df %>%
mutate(x= recode(x, "c('A', 'B')='A+B';c('D', 'E') = 'D+E'"))
# x
#1 A+B
#2 A+B
#3 A+B
#4 C
#5 D+E
#6 D+E
#7 A+B
#8 D+E
#9 C
```

```
df <- data.frame(x)
```