x <- factor(sample(letters, 20))
y <- Recode(x,
"good" = c("a","b","c"), # the old levels "a","b","c" get the new level "good"
"bad" = c("d","e","f"), # the old levels "d","e","f" get the new level "bad" etc.
"ugly" = c("g","h","k"), elselevel="other")
data.frame(x, y)
x <- factor(letters[1:6])
z1 <- Recode(x, AB=c("a","b"), CD=c("c","d"), elselevel="none of these")
z2 <- Recode(x, AB=c("a","b"), CD=c("c","d"), elselevel=NA)
z3 <- Recode(x, AB=c("a","b"), CD=c("c","d"), elselevel=NULL)
z4 <- Recode(x, AB=c("a","b"), GH=c("g","h"), elselevel=NA, use.empty=TRUE)
z5 <- Recode(x, AB=c("a","b"), GH=c("g","h"), elselevel=NA, use.empty=FALSE)
data.frame(z1, z2, z3, z4, z5)
lapply(data.frame(z1, z2, z3, z4, z5), levels)
# empty level GH exits in z4...
table(z4, useNA="ifany")
# and is dropped in z5
table(z5, useNA="ifany")
Run the code above in your browser using DataLab