R:自动计算秩和 [英] R: calculate rank sum automatically
问题描述
给定 x < - cbind(c(10,15,20,20,25,30,30,30,35,40,40,40,40,45),rep(c ('M','F'),7))
,我想自动计算类别M和F 的等级和,而不用手动。我无法想像的是当有一个领带时如何调整排名。在这种情况下,#3和#4都是20,因此共享等级值为3.5(而不是3和4)。同样#6〜#8的等级值为7,#10〜#13有11.5。没有这个调整,总和将是错误的。
Given x <- cbind(c(10,15,20,20,25,30,30,30,35,40,40,40,40,45),rep(c('M','F'),7))
, I want to calculate the rank sums of of categories M and F automatically, without doing it by hand. The thing I couldn't figure out is how to adjust the rank numbers when there is a tie. In this case, #3 and #4 are both 20 and thus share the rank value of 3.5 (instead of 3 and 4). Likewise #6 ~ #8 have the rank value of 7, and #10 ~ #13 have 11.5. Without this adjustment, the sums would be wrong.
#Wrong
sum(which(x [,2] =='F'))#= 56
sum(which(x [,2] =='M'))#= 49
#Right
sum(1,3.5,5,7,9,11.5,11.5 )#= 56.5
sum(2,3.5,7,7,11.5,11.5,14)#= 48.5
我尝试过 table()
和重复()
,但无法弄清楚如何将东西拼凑在一起。任何想法?
I've tried table()
and duplicated()
, but couldn't figure out how to piece things together. Any ideas?
编辑:我感谢konvas建议 rank()
,除了bgoldst的解决方案
My thanks to konvas for suggesting rank()
, which works in addition to bgoldst's solution.
推荐答案
您可以 sum()
rank()
与 aggregate()
:
You can sum()
the rank()
with aggregate()
:
x <- data.frame(age=c(10,15,20,20,25,30,30,30,35,40,40,40,40,45),sex=rep(c('M','F'),7));
aggregate(rank(age)~sex, x, sum );
## sex rank(age)
## 1 F 56.5
## 2 M 48.5
这篇关于R:自动计算秩和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!