[R] change some levels of a factor column in data frame according to a condition

Gesmann, Markus Markus.Gesmann at lloyds.com
Mon Nov 14 21:05:38 CET 2005


Dear R-users,

I am looking for an elegant way to change some levels of a factor column
in data frame according to a condition.
Lets look at the following data frame:

> data.frame(crit1=gl(2,5), crit2=factor(letters[1:10]), x=rnorm(10))
   crit1 crit2           x
1      1     a -1.06957692
2      1     b  0.24368402
3      1     c -0.24958322
4      1     d -1.37577955
5      1     e -0.01713288
6      2     f -1.25203573
7      2     g -1.94348533
8      2     h -0.16041719
9      2     i -1.91572616
10     2     j -0.20256478

Now I would like to find for each level in crit1 the two smallest values
of x and change the levels of crit2 to "small", so the result would look
like this:

   crit1 crit2           x
1      1     small -1.06957692
2      1     b  	0.24368402
3      1     c	 -0.24958322
4      1     small 	-1.37577955
5      1     e 	-0.01713288
6      2     f	 -1.25203573
7      2     small 	-1.94348533
8      2     h	 -0.16041719
9      2     small 	-1.91572616
10     2     j 	-0.20256478

Thank you for advice!

Markus Gesmann

************LNSCNTMCS01***************************************************
The information in this E-Mail and in any attachments is CON...{{dropped}}




More information about the R-help mailing list