I'm relatively new to R and have used stackoverflow to solve a number of problems, but this is the first time I can't find a straightforward solution. Would really appreciate help on this.
PROBLEM (simplified): I have a data frame with a field for account.id
and a field for start.date
. There may be multiple records per day. I want to create a third field called sequential.days
which reports the number of sequential days a user (account.id
) has been active.
DETAILS: The data is already sorted by account.id
and then by start.date
. There can be multiple records per day. If there are multiple records in a single day, I would like sequential.days
to populate with a value of 1.
Thanks.
Here is a "working example" -- meaning that it includes an illustration of the data and the field I'd like to generate.
id <- c(1030, 1030, 1030, 1030, 2022, 2022, 2022, 2022, 3045, 3045, 3045, 3045)
date <- c('2013-01-01', '2013-01-01', '2013-01-02', '2013-02-04', '2013-02-01', '2013-02-02', '2013-02-02', '2013-01-04', '2013-05-01', '2013-06-01', '2013-07-01', '2013-07-01')
sequential.days <- c(1,1,2,1,1,2,2,1,1,1,1,1)
df <- cbind(id, date, sequential.days)