How do I preserve continuous (1,2,3,…n) ranking notation when ranking in R?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 12:12:53

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

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

r<-rank(dat, na.last = "keep")
match(r, sort(unique(r)))

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

Use a factor and then bring it back to numeric format:

as.numeric(factor(rank(dat)))
# [1] 1 1 2 3 3 3 3 3 3 4 5 6 7 8 9
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!