[R] Collapse factor levels

Kevin E. Thorpe kevin.thorpe at utoronto.ca
Sun Nov 1 21:51:23 CET 2009


I'm sure this is simple enough, but an R site search on my subject
terms did suggest a solution.  I have a numeric vector with many
values that I wish to create a factor from having only a few levels.
Here is a toy example.

 > x <- 1:10
 > x <- 
factor(x,levels=1:10,labels=c("A","A","A","B","B","B","C","C","C","C"))
 > x
  [1] A A A B B B C C C C
Levels: A A A B B B C C C C
 > summary(x)
A A A B B B C C C C
3 0 0 3 0 0 4 0 0 0

So, there are clearly still 10 underlying levels.  The results I would
like to see from printing the value and summary(x) are:

 > x
  [1] A A A B B B C C C C
Levels: A B C
 > summary(x)
A B C
3 3 4

Hopefully this makes sense.

Thanks,

Kevin

-- 
Kevin E. Thorpe
Biostatistician/Trialist, Knowledge Translation Program
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.thorpe at utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016




More information about the R-help mailing list