[R] computing ranks in subgroups
RINNER Heinrich
H.RINNER at TIROL.GV.AT
Mon May 29 08:52:48 CEST 2000
Dear R-users!
I am using R 1.0.0 and Windows NT 4.0. I want to compute ranks for the
values of a variable, in several subgroups of a data.frame.
Currently I use:
> #example:
> x <- data.frame(group=c("A","A","B","A","B","B","A"),
val=c(10,3,9,3,5,1,7))
> x
group val
1 A 10
2 A 3
3 B 9
4 A 3
5 B 5
6 B 1
7 A 7
> x.new<-x[order(x$group), ]
> x.new$valgroupranks <- unlist(tapply(x.new$val, x.new$group, rank))
> x.new
group val valgroupranks
1 A 10 4.0
2 A 3 1.5
4 A 3 1.5
7 A 7 3.0
3 B 9 3.0
5 B 5 2.0
6 B 1 1.0
>
This seems to work, but I wonder if there is a more "natural" (simple?) way
to do that?
Heinrich..
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list