[R] Odp: group factor levels

Petr PIKAL petr.pikal at precheza.cz
Wed Feb 3 15:06:24 CET 2010


Hi

r-help-bounces at r-project.org napsal dne 03.02.2010 14:52:02:

> Dear list,
> 
> I cannot find an elegant solution to this problem. I have a factor f
> containing several levels (5) and I wish to create a new factor of the
> same length with fewer levels (2). This new factor should therefore
> group together some levels of the original data. Ideally this grouping
> would be at random, i.e I would not group together the first 2 levels
> of f, then the following 3, etc.
> 
> Below is a minimal example (my real problem has more levels, otherwise
> I would do the operation manually...)
> 
> f <- factor(rep(sample(letters[1:5], 20, repl=TRUE), each=10))
> 
> # permute the levels in random order
> disorder <- sample(levels(f), length(levels(f)))
> 
> # new levels matching the old ones
> new.lev <- rep(LETTERS[1:2], length=length(disorder))
> 
> # associate old levels to new ones
> groups <- split(disorder, new.lev)
> 
> # test each element of f for its new category
> test <- lapply(groups, function(g) f %in% g)
> 
> # f2 is the new factor, initialized with f
> f2 <- as.character(f)
> 
> # recursively modify f2
> sapply(seq_along(test), function(ii) f2[test[[ii]]] <<- names(test[ii]))
> 
> # make it a factor
> f2 <- factor(f2)
> 
> Any suggestions are very welcome, I must have missed something more 
obvious!

# order levels
f.t<-factor(f, levels=disorder)
# change levels
levels(f.t) <- new.lev
all.equal(f.t,f2)
[1] TRUE


Regards
Petr 


> 
> Best regards,
> 
> baptiste
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list