I have a data.table like so:
id date
02 2020-08-27
02 2020-09-07
04 2020-08-27
07 2020-08-27
07 2020-08-27
19 2020-08-28
19 2020-09-07
19 2020-09-07
I want to add a column that will be a sequential count based on the date
group within each id
. So, the rows with the earliest date within each id
will be 1
, then 2
for the next later date, and so on.
The result would appear like this:
id date sequence
02 2020-08-27 1
02 2020-09-07 2
04 2020-08-27 1
07 2020-08-27 1
07 2020-08-27 1
19 2020-08-28 1
19 2020-09-07 2
19 2020-09-07 2
Data:
structure(list(id = c("02", "02", "04", "07", "07", "19", "19",
"19"), date = structure(c(18501, 18512, 18501, 18501, 18501,
18502, 18512, 18512), class = "Date")), row.names = c(NA, -8L
), class = c("data.table", "data.frame"))