R中事件计算后的时间

kokeuurv  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(83)

我有下面的表格。我想计算自最近一次接受以来的天数。因此,例如,用户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)
7rfyedvj

7rfyedvj1#

我们可以通过设置'invitationDate'的子集来创建逻辑索引,其中'status_name'是Accepted,获取max并检查invitationDate

library(dplyr)
df %>% 
  mutate(mostRecent = invitationDate %in%
     max(invitationDate[status_name == "Accepted"]),
   Diff = Sys.Date() - invitationDate[mostRecent]) %>%
  ungroup

相关问题