Melissa
Melissa

Reputation: 15

How to add a rank column to sorted dates in R?

I used this code in R:

df[with(df,order(ID,Date)),]

to order the date values for each distinct ID in my data frame. Now I want to add a rank column (1 to n) next to the ordered date values, where rank = 1 is the oldest date and rank = n is the most recent date for each distinct ID.

I have seen questions about adding a rank column but not when sorting with a date value. How do I add this rank column using my code above? Thanks!

Upvotes: 1

Views: 7462

Answers (1)

eipi10
eipi10

Reputation: 93821

Here's a dplyr approach:

library(dplyr)

# Fake data
set.seed(5)
dat = data.frame(date=sample(seq(as.Date("2015-01-01"), as.Date("2015-01-31"), 
                                 "1 day"), 12),
                 ID=rep(LETTERS[1:3], c(2,6,4)))

dat %>% group_by(ID) %>%
  mutate(rank = rank(date)) %>%
  arrange(date)

         date ID rank
1  2015-01-07  A    1
2  2015-01-21  A    2
3  2015-01-03  B    1
4  2015-01-08  B    2
5  2015-01-14  B    3
6  2015-01-19  B    4
7  2015-01-20  B    5
8  2015-01-27  B    6
9  2015-01-06  C    1
10 2015-01-10  C    2
11 2015-01-22  C    3
12 2015-01-29  C    4

Upvotes: 4

Related Questions