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"),
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?