Re: [R] Conditonal Rank

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Fri, 29 Apr 2011 14:04:30 -0700

Hi:

Does this work?

library(plyr)
ddply(tmp, .(trial, Gender), transform, rankscore = rank(score))   score trial Gender rankscore

1     1     1      M         1
2     2     1      M         2
3     3     1      F         1
4     4     1      F         2
5     4     2      M         2
6     3     2      M         1
7     2     2      F         2
8     1     2      F         1

Alternatively, you could get the 'wide form' with

aggregate(score ~ trial + Gender, data = tmp, FUN = rank)   trial Gender score.1 score.2

1     1      M       1       2
2     2      M       2       1
3     1      F       1       2
4     2      F       2       1

HTH,
Dennis

On Fri, Apr 29, 2011 at 12:26 PM, Doran, Harold <HDoran_at_air.org> wrote:
> 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.
>



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 - 21:07:38 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:10:34 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