在 R 中排名时如何保留连续(1,2,3,...n)排名符号?

编程入门 行业动态 更新时间:2024-10-28 16:24:17
本文介绍了在 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)排名符号?

本文发布于:2023-10-20 12:51:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1511017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!