下面是我的数据示例(Room
和Date
)。我想生成变量Goal1
,Goal2
和Goal3
。每次Date
变量中有一个间隙就意味着房间被关闭了。我的目标是按房间识别连续日期。
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")