Given the dataset below:
tmp = data.frame(id = c(1,1,1, 2, 2, 3),
cont_date = c("2015-01-01", "2016-01-01", "2017-01-01", "2017-01-01", "2015-01-01", "2016-01-01"))
library(lubridate)
tmp$cont_date = ymd(tmp$cont_date)
I'm probably missing something obvious, but I was expecting the following code
tbl_df(tmp) %>% group_by(id) %>% arrange(cont_date) %>% mutate(seqnum = 1:length(id))
to create the sequence number of each block of records that are grouped by identifier. Instead, I get:
id cont_date seqnum
1 1 2015-01-01 1
2 2 2015-01-01 2
3 1 2016-01-01 3
4 3 2016-01-01 4
5 1 2017-01-01 5
6 2 2017-01-01 6
I do not want the sequence number of all entries, but for each block. It seems that part is being group_byignored.
Any help would be greatly appreciated.
source
share