[R] Ranking within a classification variable.
Liaw, Andy
andy_liaw at merck.com
Tue Apr 19 18:05:35 CEST 2005
Here's one way:
> dat <- data.frame(x=runif(10), f=sample(1:2, 10, replace=TRUE))
> dat
x f
1 0.06928792 1
2 0.55972448 1
3 0.39285280 1
4 0.72547126 1
5 0.69027628 1
6 0.51918727 1
7 0.62048040 2
8 0.42240036 1
9 0.90938702 1
10 0.16013661 2
> dat$rank <- with(dat, ave(x, f, FUN=rank))
> dat[order(dat$f, dat$x),] ## Just checking...
x f rank
1 0.06928792 1 1
3 0.39285280 1 2
8 0.42240036 1 3
6 0.51918727 1 4
2 0.55972448 1 5
5 0.69027628 1 6
4 0.72547126 1 7
9 0.90938702 1 8
10 0.16013661 2 1
7 0.62048040 2 2
Don't know how this rates on the slick-quick-sexy scale...
Andy
> From: Rolf Turner
>
>
> Suppose I have a data frame with two columns ``district'' and
> ``score'' --- score is numeric; district may be considered
> categorical.
>
> I wish to append to this data frame a third column whose entries are
> the ranks of ``score'' ***within*** district.
>
> I've tried fiddling about with tapply() and by() but the result is a
> list whose i-th component consists of the ranks of the scores within
> the i-th district. I then have trouble figuring out how to put these
> results into a column of the data frame in the proper order.
>
> Is there a slick-quick-sexy way of doing this (without resorting to
> looping)? Am I missing something obvious?
>
> Thanks for any help bestowed.
>
> cheers,
>
> Rolf Turner
> rolf at math.unb.ca
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
>
More information about the R-help
mailing list