dasf
dasf

Reputation: 1045

R: calculate rank sum automatically

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

I've tried table() and duplicated(), but couldn't figure out how to piece things together. Any ideas?

EDIT: My thanks to konvas for suggesting rank(), which works in addition to bgoldst's solution.

Upvotes: 3

Views: 1898

Answers (3)

Colonel Beauvel
Colonel Beauvel

Reputation: 31161

Base R, you can use ave:

setNames(unique(ave(rank(x[,1]), x[,2], FUN=sum)), unique(x[,2]))
#    M    F 
# 48.5 56.5 

Upvotes: 1

dimitris_ps
dimitris_ps

Reputation: 5951

With dplyr

library(dplyr)
x <- cbind(c(10,15,20,20,25,30,30,30,35,40,40,40,40,45),rep(c('M','F'),7))
data.frame(x) %>% mutate(rank=rank(X1)) %>% group_by(X2) %>% summarise(sum(rank))

Upvotes: 1

bgoldst
bgoldst

Reputation: 35314

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

Upvotes: 4

Related Questions