[R] Conditonal Rank

From: Doran, Harold <HDoran_at_air.org>
Date: Fri, 29 Apr 2011 15:26:47 -0400


Suppose I have data such as

tmp <- data.frame(score = c(1,2,3,4, 4,3,2,1), trial = gl(2,4), Gender = gl(2,2,8, labels=c('M', 'F')))

Now I would like to compute a rank on the variable score conditional on trial and gender. I could do

res <- with(tmp, tapply(score, list(Gender, trial), rank)) res[,1]
res[,2]

and then finagle a way to create a new variable in the dataframe tmp that has these ranks associated with the correct rows. But, perhaps there is a better way. Any suggestions?

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 29 Apr 2011 - 19:30:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Apr 2011 - 21:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive