Flag consecutive dates by group
Below is an example of my data (Room
and Date
). I would like to generate variables Goal1
, Goal2
and Goal3
. Every time there is a gap in the Date
variable means that the room was closed. My goal is to identify consecutive dates by room.
Room Date Goal1 Goal2 Goal3
1 Upper A 2021-01-01 1 2021-01-01 2021-01-02
2 Upper A 2021-01-02 1 2021-01-01 2021-01-02
3 Upper A 2021-01-05 2 2021-01-05 2021-01-05
4 Upper A 2021-01-10 3 2021-01-10 2021-01-10
5 Upper B 2021-01-01 1 2021-01-01 2021-01-01
6 Upper B 2021-02-05 2 2021-02-05 2021-02-07
7 Upper B 2021-02-06 2 2021-02-05 2021-02-07
8 Upper B 2021-02-07 2 2021-02-05 2021-02-07
df - data.frame(Area = c(Upper A, Upper A, Upper A, Upper A,
Upper B, Upper B, Upper B, Upper B),
Date = c(1/1/2021, 1/2/2021, 1/5/2021, 1/10/2021,
1/1/2021, 2/5/2021, 2/6/2021, 2/7/2021))
df$Date - as.Date(df$Date, format = %m/%d/%Y)
Category Data Science