在 R 中排名与关系 [英] Rank with Ties in R

查看:31
本文介绍了在 R 中排名与关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份比赛的参赛者名单和积分值.许多竞争对手都被捆绑在一起.我需要一种在 R 中对它们进行排名的方法,例如,如果四个竞争对手并列第四,输出可能如下所示:

I have a list of competitors and point values from a tournament. A lot of competitors are tied. I need a way to rank them in R so that, for example, if four competitors tie for fourth place, the output might look like this:

Competitor A: 1 
Competitor B: 2
Competitor C: 3
Competitor D: 4
Competitor E: 4
Competitor F: 4
Competitor G: 4
Competitor F: 8

所以并列之后的下一个排名不是 5,而是 8(因为 4-7 位有四个参赛者).

So the next rank after the tie, instead of being 5, would be 8 (because four competitors took up the slots from 4-7).

我知道这有点违反直觉,但如果有人有任何想法,我们将不胜感激.谢谢!

I know this is a little counter intuitive, but if anyone has any ideas it would be highly appreciated. Thanks!

数据

v1 <- setNames(c(4, 5, 2, 7, 7, 7, 7, 9), paste('Competitor', LETTERS[1:8]))

推荐答案

我们可以使用 min_rank from dplyr

We can use min_rank from dplyr

library(dplyr)
min_rank(v1)
#[1] 2 3 1 4 4 4 4 8

或者在 base R 中,(来自@Steven Beaupré 的评论)

Or in base R, (from @Steven Beaupré's comments)

rank(v1, ties.method = "min")

数据

v1 <- c(4, 5, 2, 7, 7, 7, 7, 9)

这篇关于在 R 中排名与关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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