Re: [R] sort the values of members and obtain their ranks within groups

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Tue 23 May 2006 - 12:04:03 EST


See if this does what you want:  

> cbind(df, rank=ave(df$member, df$group, FUN=rank))
  group member rank

1     1     30    3
2     1     10    1
3     1     22    2
4     5     21    2
5     5     44    3
6     5     15    1

Andy


From: r-help-bounces@stat.math.ethz.ch on behalf of Zhen Zhang Sent: Mon 5/22/2006 6:40 PM
To: R-help@stat.math.ethz.ch
Subject: [R] sort the values of members and obtain their ranks within groups [Broadcast]

Dear all,

I would like to sort the values of "member" for each group, and obtain a variable to indicate its rank within the group.

For example, we have original dataset as follows:

df <- data.frame(group = c(rep(1, 3), rep(5, 3)),  member = c(30, 10, 22, 21, 44, 15))

  group member

1     1     30 
2     1     10 
3     1     22 
4     5     21 
5     5     44 
6     5     15 

After sorting the variable "member", we want the dataset like this:

  group member order

1     1     30     3 
2     1     10     1 
3     1     22     2 
4     5     21     2 
5     5     44     3 
6     5     15     1 

I already searched the R helper archives, but failed to find the proper answers.

Could you please kindly give me some suggestions, except using loop because of larger sample size? Thanka a lot.

Best regards,

Zhen Zhang

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html <http://www.R-project.org/posting-guide.html>

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue May 23 12:09:47 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 23 May 2006 - 14:10:31 EST.

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