Skip to content
Prev 256993 / 398506 Next

modify particular factor levels

baptiste auguie-5 wrote:
The split-and-unite policy you use makes it a bit difficult to re-unite. You
could keep a copy of the original sequence, but it's probably easier to do
it via match. Note also that it also works for simple named vectors, which I
would prefer (I hate these generic lists, lapply).

You should probably think of the error case below. Maybe it is best to keep
it an error.

Dieter



modify.levels <- function(f, modify=list()){
  levs = levels(f)
  m = match(modify,levs)
  levs[m] = names(modify)
  factor(f,labels=levs)
}

f <- factor(c(LETTERS[1:4],LETTERS[1:2]))# Added a few more to avoid special
case
modify.levels(f, list(aa = "A", cc="C"))
modify.levels(f, c(aa = "A", cc="C"))
modify.levels(f, c(aa = "G", cc="C")) # Gives an error



--
View this message in context: http://r.789695.n4.nabble.com/modify-particular-factor-levels-tp3450862p3452417.html
Sent from the R help mailing list archive at Nabble.com.