在 R 中排名时如何保留连续(1,2,3,...n)排名符号? [英] How do I preserve continuous (1,2,3,...n) ranking notation when ranking in R?

查看:46
本文介绍了在 R 中排名时如何保留连续(1,2,3,...n)排名符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想使用共享案例(又名关系)的最低排名对一组数字进行排名:

If I want to rank a set of numbers using the minimum rank for shared cases (aka ties):

dat <- c(13,13,14,15,15,15,15,15,15,16,17,22,45,46,112)
rank(dat, ties = 'min')

我得到了结果:

 1  1  3  4  4  4  4  4  4 10 11 12 13 14 15

但是,我希望排名是由 1,2,3,...n 组成的连续序列,其中 n 是独特的排名.

However, I want the rank to be a continuous series consisting of 1,2,3,...n, where n is the number of unique ranks.

有没有办法使 rank(或类似函数)通过​​将关系分配给上述最低等级来对一系列数字进行排名但是 而不是通过之前的关系数跳过后续排名值到 而是继续从之前的排名开始排名?

Is there a way to make rank (or a similar function) rank a series of numbers by assigning ties to the lowest rank as above but instead of skipping subsequent rank values by the number of previous ties to instead continue ranking from the previous rank?

例如,我希望上面的排名结果:

For example, I would like the above ranking to result in:

1  1  2  3  3  3  3  3  3  4  5  6  7  8  9

推荐答案

你可以使用 dplyr:

you could do it using dplyr:

library(dplyr)
dense_rank(dat)

 [1] 1 1 2 3 3 3 3 3 3 4 5 6 7 8 9

如果您不想加载整个库并在基本 r 中执行:

if you don't want to load the whole library and do it in base r:

match(dat, sort(unique(dat)))

 [1] 1 1 2 3 3 3 3 3 3 4 5 6 7 8 9

这篇关于在 R 中排名时如何保留连续(1,2,3,...n)排名符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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