Search code examples
rranking

Rank by multiple columns in R


Trying to create a rank indicator over 2 columns, in this case both account and DATE.

For example:


df <- data.frame(
        Account = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3),
        DATE = c(201901, 201902, 201903, 201904, 201902, 201903, 201904, 201905, 201906, 201907, 201904, 201905))

> df
   Account   DATE
         1 201901
         1 201902
         1 201903
         1 201904
         2 201902
         2 201903
         2 201904
         2 201905
         2 201906
         2 201907
         3 201904
         3 201905

I've tried to use rank and order, and rank(rank()) and order(order()) but with no luck

df <- df %>%
  mutate("rank" = rank(Account, DATE))
   Account   DATE rank
         1 201901  2.5
         1 201902  2.5
         1 201903  2.5
         1 201904  2.5
         2 201902  7.5
         2 201903  7.5
         2 201904  7.5
         2 201905  7.5
         2 201906  7.5
         2 201907  7.5
         3 201904 11.5
         3 201905 11.5

But what I want is for it to rank the dates descending, but by each account, it should look like this:

Account   DATE RANK
      1 201901    4
      1 201902    3
      1 201903    2
      1 201904    1
      2 201902    6
      2 201903    5
      2 201904    4
      2 201905    3
      2 201906    2
      2 201907    1
      3 201904    2
      3 201905    1

Solution

  • library("dplyr")
    
    df %>% 
        group_by(Account) %>% 
        mutate("rank" = rank(DATE))
    #> # A tibble: 12 x 3
    #> # Groups:   Account [3]
    #>    Account   DATE  rank
    #>      <dbl>  <dbl> <dbl>
    #>  1       1 201901     1
    #>  2       1 201902     2
    #>  3       1 201903     3
    #>  4       1 201904     4
    #>  5       2 201902     1
    #>  6       2 201903     2
    #>  7       2 201904     3
    #>  8       2 201905     4
    #>  9       2 201906     5
    #> 10       2 201907     6
    #> 11       3 201904     1
    #> 12       3 201905     2
    

    Created on 2020-03-09 by the reprex package (v0.3.0.9001)