如果我想使用共享案例(又名关系)的最低排名对一组数字进行排名:
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)排名符号?
发布评论