R:确定每个日期间隔是否与数据框中的所有其他日期间隔重叠 [英] R: Determine if each date interval overlaps with all other date intervals in a dataframe
问题描述
对于数据框中的每个日期间隔行,我想确定它是否与所有其他日期间隔重叠.排除自身.
For each date interval row in my dataframe, I would like to determine whether it overlaps with all other date intervals or not. Excluding itself.
带有开始日期和结束日期的数据框,表示时间间隔:
A dataframe with start and end date, representing intervals:
`data <- read.table(header=TRUE,text="
start.date end.date
2019-09-01 2019-09-10
2019-09-05 2019-09-07
2019-08-25 2019-09-05
2019-10-10 2019-10-15
")`
此函数 lubridate :: int_overlaps()
通过返回逻辑TRUE或FALSE,检查两个日期间隔是否重叠.
This function lubridate::int_overlaps()
checks if two date intervals overlap or not by returning logical TRUE or FALSE.
`int_overlaps(interval(ymd("2019-09-01"),ymd("2019-09-10")), interval(ymd("2019-09-05"), ymd("2019-09-07")))
[1] TRUE
int_overlaps(interval(ymd("2019-09-01"),ymd("2019-09-10")), interval(ymd("2019-10-10"), ymd("2019-10-15")))
[1] FALSE`
我想使用int_overlap()迭代每个日期间隔与除自身之外的所有其他日期间隔,以确定它是否与其他日期重叠.
I would like to iterate each date interval with the all other date intervals excluding itself using int_overlap() to determine whether it overlaps with other date or not.
输出应如下所示:
`data <- read.table(header=TRUE,text="
start.date end.date overlaps
2019-09-01 2019-09-10 TRUE
2019-09-05 2019-09-07 TRUE
2019-08-25 2019-09-05 TRUE
2019-10-10 2019-10-15 FALSE
")
`
推荐答案
这里是使用 dplyr
和 purrr
的一个选项,我们遍历 Int
的索引将当前间隔与其他间隔进行比较.
Here is one option using dplyr
and purrr
, we loop through Int
's indexes comparing the current interval with the other intervals.
library(dplyr)
library(purrr)
library(lubridate)
data %>% mutate(Int = interval(start.date, end.date),
overlaps = map(seq_along(Int), function(x){
#browser()
#Get all Int indexes other than the current one
y = setdiff(seq_along(Int), x)
#The interval overlaps with all other intervals
#return(all(int_overlaps(Int[x], Int[y])))
#The interval overlaps with any other intervals
return(any(int_overlaps(Int[x], Int[y])))
}))
start.date end.date Int overlaps
1 2019-09-01 2019-09-10 2019-09-01 UTC--2019-09-10 UTC TRUE
2 2019-09-05 2019-09-07 2019-09-05 UTC--2019-09-07 UTC TRUE
3 2019-08-25 2019-09-05 2019-08-25 UTC--2019-09-05 UTC TRUE
4 2019-10-10 2019-10-15 2019-10-10 UTC--2019-10-15 UTC FALSE
这篇关于R:确定每个日期间隔是否与数据框中的所有其他日期间隔重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!