Calculate rank with ties based on more than one variable

旧城冷巷雨未停 提交于 2019-11-29 15:42:52

You may use the data.table equivalent of base::rank, frank. A nice feature with frank is that it accepts, not only vectors (as in rank), but also a data.frame or a data.table as input. For these types of objects, the rank may be based on several columns.

Using your original data.frame:

test$rank <- data.table::frank(test, -gold, -silver, -bronze, ties.method = "min")

Or if you want to go all in with data.table functions:

setDT(test)[ , rank := frank(test, -gold, -silver, -bronze, ties.method = "min")]
setorder(test, rank)

A base R solution would be:

test <- data.frame("ID"=c("1_1", "1_2", "1_3", "1_4","1_5","1_6"), 
                   "gold"=c(10,4,1,7,7,1), 
                   "silver"=c(1,3,2,19,19,2), 
                   "bronze"=c(1,8,2,0,0,2))

(test_ordered<-with(test, test[order(-gold,-silver,-bronze),]))

roll.any.greater <- function (mat) {
  mat.lead <- head(mat, -1)
  mat.lag <- tail(mat, -1)
  result <- rep(1, nrow(mat.lead) + 1)
  for (i in (2:length(result))) {
    result[i] <- ifelse(any(as.logical(abs(mat.lead[i-1, ] - mat.lag[i-1, ]))) != FALSE,
                        i, result[i-1])
  }
  return(result)
}
(want <- cbind(test_ordered,
               rank =
               roll.any.greater(test_ordered[colnames(test_ordered) %in% c("gold", "silver", "bronze")])))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!