Idiom for ifelse-style recoding for multiple categories

You could convert your variable to a factor and change its levels by levels<- function. In one command it could be like:

`levels<-`(
    factor(dat$product),
    list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12)
)

In steps:

brands <- factor(dat$product)
levels(brands) <- list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12)

Leave a Comment