Search code examples
rdataframedata-manipulationrankingmultiple-conditions

ranking dataframe using two columns in R


I am new to using R, and I am having hard time trying to rank dataframe using two columns in R. The data is in the form of this.

A B
1 1
2 1
2 1
4 4
5 3

I want result to be in the form of

A B Rank
1 1 1
2 1 2
2 1 2
4 4 5
5 3 4

which is ranked by B first, and the A is used to rank if there is equal value in B. I think my question is very similar to How to rank rows by two columns at once in R? and I tried the answers for this, but this didn't work for me.


Solution

  • You can use data.table::frank or dplyr::min_rank:

    data.table::frank

    dt$Rank <- frank(dt, B, A, ties.method = "min")
    dt
      A B Rank
    1 1 1    1
    2 2 1    2
    3 2 1    2
    4 4 4    5
    5 5 3    4
    

    dplyr::min_rank

    mutate(dt, Rank = min_rank(paste(B,A)))
      A B Rank
    1 1 1    1
    2 2 1    2
    3 2 1    2
    4 4 4    5
    5 5 3    4
    

    Data

    dt <- data.frame(A = c(1,2,2,4,5), B = c(1,1,1,4,3))