pssguy
pssguy

Reputation: 3505

How can I rank on more than one variable in dplyr

If I have

library(dplyr)
 df <- data.frame(name=c("A","B","C","D"),value1=c(8,9,8,10),value2=c(1,2,3,4))
 df 
      name value1 value2
   1    A      8      1
   2    B      9      2
   3    C      8      3
   4    D     10      4 

 # I want to do something like this without the error

newdf <- df %>%
mutate(rank=row_number(desc(value1),desc(value2)))

newdf
    name value1 value2 rank
1    A      8      1    4
2    B      9      2    2
3    C      8      3    3
4    D     10      4    1

How can I rank the rows based on one column and use a second column in case of ties?

Upvotes: 2

Views: 190

Answers (2)

Mahbubul Majumder
Mahbubul Majumder

Reputation: 340

The following codes will produce the same result you posted in the question. This is what the row_number() will return but your original data don't have to be rearranged.

newdf <- df %>%
  mutate(rank=order(-value1,-value2))

Please Note: if you want dense_rank this code will not do that.

Upvotes: 1

pssguy
pssguy

Reputation: 3505

Now I have had a look further, I think this does the trick

df %>% arrange(desc(value1),desc(value2)) %>% mutate(rank=row_number())

Upvotes: 2

Related Questions