[R] Computing an ordering on subsets of a data frame

Lukas Biewald lukeb at powerset.com
Wed Apr 18 23:49:23 CEST 2007


If I have a data frame X that looks like this:

A B
- -
1 2
1 3
1 4
2 3
2 1
2 1
3 2
3 1
3 3

and I want to make another column which has the rank of B computed
separately for each value of A.

I.e. something like:

A B C
- - -
1 2 1
1 3 2
1 4 3
2 3 3
2 1 1
2 1 2
3 2 2
3 1 1
3 3 3

by(X, X[,1], function(x) { rank(x[,1], ties.method="random") } ) almost
seems to work, but the data is not in a frame, and I can't figure out how to
merge it back into X properly.

Thanks,
Lukas



More information about the R-help mailing list