我有下面的表格。我想计算自最近一次接受以来的天数。因此,例如,用户1在多个场合接受,但其中只有一个是最近的。我想做两件事:
标记一个表示mostRecent == TRUE/FALSE
的简单真值,并计算从接受到今天的日期的天数。
user status_name invitationDate
<fct> <fct> <date>
1 1 Accepted 2021-09-09
2 1 Declined 2021-09-10
3 1 Accepted 2021-09-30
4 4 Accepted 2021-11-10
5 1 Accepted 2021-11-22
6 4 Declined 2021-11-29
我已经包含了重新创建下表的代码。
df <- tribble(
~user, ~status_name, ~invitationDate,
"1", "Declined", "2021-07-13",
"4", "Declined", "2021-07-31",
"1", "Accepted", "2021-09-09",
"1", "Declined", "2021-09-10",
"1", "Accepted", "2021-09-30",
"4", "Accepted", "2021-11-10",
"3", "Declined", "2021-11-12",
"2", "Declined", "2021-11-18",
"1", "Accepted", "2021-11-22",
"4", "Declined", "2021-11-29"
) %>%
mutate(
user = as.factor(user),
status_name = as.factor(status_name),
invitationDate = as.Date(invitationDate, format = "%Y-%m-%d")
) %>%
group_by(user) %>%
mutate(cumsum = cumsum(status_name == "Accepted")) %>%
filter(cumsum > 0) %>%
select(-cumsum)
1条答案
按热度按时间7rfyedvj1#
我们可以通过设置'invitationDate'的子集来创建逻辑索引,其中'status_name'是
Accepted
,获取max
并检查invitationDate