[R] Coarsening Factors

Murray Jorgensen maj at waikato.ac.nz
Thu Sep 8 23:59:58 CEST 2005


It is not uncommon to want to coarsen a factor by grouping levels 
together. I have found one way to do this in R:

 > sites
  [1] F A A D A A B F C F A D E E D C F A E D F C E D E F F D B C
Levels: A B C D E F
 > regions <- list(I = c("A","B","C"), II = "D", III = c("E","F"))
 > library(Epi)
 > region <- Relevel(sites,regions)
 > region
  [1] III I   I   II  I   I   I   III I   III I   II  III III II  I 
III I   III
[20] II  III I   III II  III III III II  I   I
Levels: I II III

However this seems like using a sledgehammer to crack a nut. Can someone 
suggest a simpler way to do this task?

Murray Jorgensen
-- 
Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: maj at waikato.ac.nz                                Fax 7 838 4155
Phone  +64 7 838 4773 wk     Home +64 7 825 0441   Mobile 021 1395 862




More information about the R-help mailing list