Search code examples
rfor-loopranking

Calculate rank by each column in dataframe in R


I tried to generate a ranking (highest to lowest) by each column of a dataframe. I have this data:

      [,Cod] [,2] [,3] [,4]  
[1,]   A     100  300   200  
[2,]   B     200  200   100  
[3,]   C     300  100   300  

I use this code to calculate the ranking (only one column):

df$rankCol2 <- (length(df$Col1)+1)-rank(df$Col2,ties.method = "last")

but I need a solution that works for several columns. Like this:

  [,Cod] [,2] [,3] [,4]  [,RankCol2] [,RankCol3] [,RankCol4] 
[1,]   A     100  300   200     3            1          2
[2,]   B     200  200   100     2            2          3
[3,]   C     300  100   300     1            3          1

Thks


Solution

  • If you want to rank the data column-wise, you can use apply, thus:

    DATA:

    df <- data.frame(
      cod = c("A", "B", "C", "D", "E"),
      v1 = c(100, 300, 400, 200, 700),
      v2 = c(500, 600, 900, 100, 200),
      v3 = c(300, 1000, 200, 400, 500))
    

    SOLUTION:

    df[paste0("v", 1:3, "rank")] <- apply(-df[,2:4], 2, rank)
    

    RESULT:

    df
      cod  v1  v2   v3 v1rank v2rank v3rank
    1   A 100 500  300      5      3      4
    2   B 300 600 1000      3      2      1
    3   C 400 900  200      2      1      5
    4   D 200 100  400      4      5      3
    5   E 700 200  500      1      4      2