Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate ranks for each group

Tags:

r

ranking

plyr

I have a df with types and values. I want to rank them in order of x within type and give a count of how many other rows row n has higher value of x than (column pos).

e.g.

df <- data.frame(type = c("a","a","a","b","b","b"),x=c(1,77,1,34,1,8))
# for type a row 3 has a higher x than row 1 and 2 so has a pos value of 2

I can do this with:

library(plyr)
df <- data.frame(type = c("a","a","a","b","b","b"),x=c(1,77,1,34,1,8))
df <- ddply(df,.(type), function(x) x[with(x, order(x)) ,])
df <- ddply(df,.(type), transform, pos = (seq_along(x)-1) )

     type  x pos
1    a  1   0
2    a  1   1
3    a 77   2
4    b  1   0
5    b  8   1
6    b 34   2

But this approach does not take into account ties between type a row 1 and 2. Whats the easiest way to get the output where ties have the same value e.g.

     type  x pos
 1    a  1   0
 2    a  1   0
 3    a 77   2
 4    b  1   0
 5    b  8   1
 6    b 34   2
like image 855
user1320502 Avatar asked Dec 17 '12 14:12

user1320502


People also ask

How do you rank in a group?

There is a formula to quickly rank values based on group. Select a blank cell next to the data, C2 for instance, type this formula, =SUMPRODUCT(($A$2:$A$11=A2)*(B2<$B$2:$B$11))+1 then drag autofill handle down to apply this formula to the cells you need.

What is the formula for ranking in Excel?

=RANK(B2,$B$2:$B$11) For ascending order, type a 1, or any other number except zero. If you were comparing golf scores, you could type a 1, to rank in ascending order.


1 Answers

ddply(df,.(type), transform, pos = rank(x,ties.method ="min")-1)

  type  x pos
1    a  1   0
2    a 77   2
3    a  1   0
4    b 34   2
5    b  1   0
6    b  8   1
like image 135
Roland Avatar answered Oct 17 '22 05:10

Roland