【发布时间】:2021-08-29 05:02:35
【问题描述】:
我有一个关于在多个条件下分组行的问题的后续问题 (Previous question)。
我想知道如何在第一次约会后的 31 天内对观察结果进行分组。更重要的是,在 31 天过后,同一组中的下一个日期将是该组的“新”第一个日期。此外,在每次“购买”后,分组也应该停止,购买后的下一个观察将是该组的“新”第一天。
让我用一个例子来说明它:
example <- structure(
list(
userID = c(1,1,1,1,1,1,2,2,2,2),
date = structure(
c(
18168, #2019-09-29
18189, #2019-10-20
18197, #2019-10-28
18205, #2019-11-05
18205, #2019-11-05
18217, #2019-11-17
18239, #2019-12-09
18270, #2020-01-09
18271, #2020-01-10
18275 #2020-01-14
),
class = "Date"
),
purchase = c(0,0, 0, 0, 0, 1, 0, 0, 1, 0)
),
row.names = c(NA, 10L),
class = "data.frame"
)
期望的结果:
Outcome <- data.frame(
userID = c(1,1,2,2,2),
date.start = c("2019-09-29", "2019-11-05", "2019-12-09", "2020-01-10", "2020-01-14"),
date.end = c("2019-10-28", "2019-11-17", "2020-01-09", "2020-01-10", "2020-01-14"),
purchase = c(0, 1, 0, 1, 0)
)
提前致谢! :)
【问题讨论】:
标签: r dplyr grouping multiple-conditions