Skip to content
Prev 200627 / 398503 Next

recode according to old levels

Andreas Wittmann wrote:
I assume that you mean the recode() function in pkg:car?
(You really should say so.)

Here are a couple of ways to recode the factor:

set.seed(6931)
F <- factor(sample(rep(c("A","B","C"), c(4,2,5))))
lev <- levels(F)

# 1: using car:::recode
F <- recode(F, "lev[c(1,3)] = 'X'; else = 'Y'")

# 2: using base:::levels
levels(F) <- list(X = lev[c(1,3)], Y = lev[-c(1,3)])

  -Peter Ehlers