Home > OS >  Ranking dataframe in reverse date order
Ranking dataframe in reverse date order

Time:07-20

I'm trying to rank a data frame based on reverse order for dates. For example, see below:

user start_time
ID 2022-05-31 18:13:43
BB 2020-11-07 00:42:36
RL 2021-11-25 01:01:15
ID 2020-05-30 03:30:19
KF 2021-05-18 00:26:32
MT 2021-07-02 22:44:01
BB 2021-08-01 16:06:37
RL 2021-02-02 20:54:08
BB 2022-06-25 22:52:56
KF 2021-04-24 01:45:18

The reverse order rank should be:

user start_time rank
ID 2022-05-31 18:13:43 1
BB 2020-11-07 00:42:36 3
RL 2021-11-25 01:01:15 1
ID 2020-05-30 03:30:19 2
KF 2021-05-18 00:26:32 1
MT 2021-07-02 22:44:01 1
BB 2021-08-01 16:06:37 2
RL 2021-02-02 20:54:08 2
BB 2022-06-25 22:52:56 1
KF 2021-04-24 01:45:18 2

CodePudding user response:

One

do.call(
  rbind,
  lapply(split(df,df$user),function(x){
    cbind(x,"rank"=order(x$start_time,decreasing=T))
  })
)

      user          start_time rank
BB.2    BB 2020-11-07 00:42:36    3
BB.7    BB 2021-08-01 16:06:37    2
BB.9    BB 2022-06-25 22:52:56    1
ID.1    ID 2022-05-31 18:13:43    1
ID.4    ID 2020-05-30 03:30:19    2
KF.5    KF 2021-05-18 00:26:32    1
KF.10   KF 2021-04-24 01:45:18    2
MT      MT 2021-07-02 22:44:01    1
RL.3    RL 2021-11-25 01:01:15    1
RL.8    RL 2021-02-02 20:54:08    2

CodePudding user response:

Using dplyr,

library(dplyr)
df %>%
  group_by(user) %>%
  mutate(rank = order(start_time, decreasing = T))

   user  start_time           rank
   <chr> <chr>               <int>
 1 ID    2022-05-31 18:13:43     1
 2 BB    2020-11-07 00:42:36     3
 3 RL    2021-11-25 01:01:15     1
 4 ID    2020-05-30 03:30:19     2
 5 KF    2021-05-18 00:26:32     1
 6 MT    2021-07-02 22:44:01     1
 7 BB    2021-08-01 16:06:37     2
 8 RL    2021-02-02 20:54:08     2
 9 BB    2022-06-25 22:52:56     1
10 KF    2021-04-24 01:45:18     2
  •  Tags:  
  • r
  • Related