Skip to content
Prev 19863 / 63421 Next

Collapsing levels of a factor

I find particulary usefull the list approach. Here is the example from
levels help page. I like this approach, since you can modify list and
apply it to a factor via levels.

  ## we can add levels this way:
     f <- factor(c("a","b"))
     levels(f) <- c("c", "a", "b")
     f

     f <- factor(c("a","b"))
     levels(f) <- list(C="C", A="a", B="b")
     f

I was playing around this last week and wrote a simple function[1],
which can save you some work in getting such a list from a factor. Is R
core interested in it?

[1]http://www.bfro.uni-lj.si/MR/ggorjan/software/R/ggmisc/factorMap.R