How to get ranks with no gaps when there are ties among values?

后端 未结 8 1297
感情败类
感情败类 2020-12-03 14:26

When there are ties in the original data, is there a way to create a ranking without gaps in the ranks (consecutive, integer rank values)? Suppose:

x <-           


        
8条回答
  •  无人及你
    2020-12-03 15:18

    I can think of a quick function to do this. It's not optimal with a for loop but it works:)

    x=c(1,1,2,3,4,5,8,8)
    
    foo <- function(x){
        su=sort(unique(x))
        for (i in 1:length(su)) x[x==su[i]] = i
        return(x)
    }
    
    foo(x)
    
    [1] 1 1 2 3 4 5 6 6
    

提交回复
热议问题