Rank per row over multiple columns in R

后端 未结 2 2001
猫巷女王i
猫巷女王i 2020-12-06 02:01

I\'m using R for the analysis of my masterthesis. Unfortunately, I got stuck with this problem:

I would like to compute a new variable which calculates the rank of

2条回答
  •  被撕碎了的回忆
    2020-12-06 02:40

    ?rank and see how to handle ties.

    x <- cbind(a=c(11,22,44),
           b=c(21,12,22),
           c=c(35,66,12))
    > x
          a  b  c
    [1,] 11 21 35
    [2,] 22 12 66
    [3,] 44 22 12
    

    EDITED 2018-10-22

    Now, rank on

    rows

     t(apply(-x, 1, rank))
         a b c
    [1,] 3 2 1
    [2,] 2 3 1
    [3,] 1 2 3
    

    or columns

    apply(x, 2, rank)
         a b c
    [1,] 3 1 3
    [2,] 2 2 1
    [3,] 1 3 2
    

提交回复
热议问题