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

前端 未结 2 1825

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, t         


        
相关标签:
2条回答
  • 2020-12-06 23:07

    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:

    match(dat, sort(unique(dat)))
    
     [1] 1 1 2 3 3 3 3 3 3 4 5 6 7 8 9
    
    0 讨论(0)
  • 2020-12-06 23:11

    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
    
    0 讨论(0)
提交回复
热议问题