计算每个组的等级 [英] Calculate ranks for each group

查看:89
本文介绍了计算每个组的等级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有类型和值的df.我想在type中以x的顺序对它们进行排名,并计算出n行中的x值比(clumn pos)值高的其他行数.

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).

例如

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

我可以这样:

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

但是这种方法没有考虑类型a第1行和第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

推荐答案

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

这篇关于计算每个组的等级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆