给定数据集

structure(list(intervention = c("Self Isolation", "Lockdown Low",
"Lockdown Low", "Self Isolation", "Social Distancing", "Lockdown Low",
"Social Distancing", "Handwashing"), date_start = structure(c(17897,
17957, 18444, 17987, 17897, 17532, 17942, 18018), class = "Date"),
date_end = structure(c(17956, 18262, 18475, 18017, 17956,
18053, 18017, 18048), class = "Date")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L))如何检查“干预”是否有重叠日期?在这个例子中,所有的干预措施都是好的,但是“社会距离”和“封闭低”。
理想的输出将是一个数据框架,每行有一个干预,列中填充TRUE/FALSE,这取决于干预是否存在重叠。

( tidyverse解的额外点数。)
发布于 2020-04-20 22:59:53
我们可以做summarise
library(dplyr)
df1 %>%
arrange(intervention, date_start, date_end) %>%
group_by(intervention) %>%
summarise(overlapping = any(date_start < lag(date_end,
default = first(date_end)) & row_number() != 1))
# A tibble: 4 x 2
# intervention overlapping
# <chr> <lgl>
#1 Handwashing FALSE
#2 Lockdown Low TRUE
#3 Self Isolation FALSE
#4 Social Distancing TRUE https://stackoverflow.com/questions/61333135
复制相似问题