确定三个(或更多)给定时间范围在R中是否重叠 [英] Determine if three (or more) given time ranges overlap in R
问题描述
我有三个数据帧( df1
, df2
和 df3
),其开始日期和结束日期指示事件发生的时间.我知道如何确定 df1
和 df2
或 df3
之间是否发生重叠,但是我不知道如何确定所有之间何时发生重叠数据帧.理想情况下,如果 df2
和 df3
在 df1
的开始日期和结束日期之内,则可以通过新列 Overlap
,当所有三个数据帧在时间上重叠时,结果为 TRUE
;当所有三个数据帧在时间上不重叠时,结果为 FALSE
.
I have three data frames (df1
, df2
, and df3
) with start and end dates that indicate when an event has occurred. I know how to determine if overlap occurs between df1
and either df2
or df3
but I don't know how to determine when overlap occurs between all data frames. Ideally, if df2
and df3
are within the start and end dates of df1
then this would be indicated by a new column Overlap
with results TRUE
when all three data frames overlap in time and FALSE
when all three data frames do not overlap in time.
> df1$aa
date_start date_end Site Variable
1 2002-04-12 2002-04-21 aa Temp1
2 2002-06-26 2002-07-05 aa Temp1
3 2002-08-15 2002-08-20 aa Temp1
4 2005-08-08 2005-08-19 aa Temp1
> df2$bb
date_start date_end Site Variable
1 2002-04-13 2002-04-19 aa Temp2
2 2002-08-11 2002-08-19 aa Temp2
3 2005-06-09 2005-06-14 aa Temp2
4 2005-08-10 2005-08-14 aa Temp2
> df2$cc
date_start date_end Site Variable
1 2002-04-14 2002-04-19 aa Temp3
2 2002-08-11 2002-08-19 aa Temp3
3 2005-06-09 2005-06-14 aa Temp3
4 2005-08-10 2005-08-14 aa Temp3
此代码确定 df1
和 df2
OR df3
之间是否存在重叠.
This code determines if there is overlap between df1
and df2
OR df3
.
df1$aa$Overlap <- df1$aa$date_start %in% unlist(Map(':', df2$aa$date_start, df2$aa$date_end))
理想地,上面的代码将能够合并 df1
, df2
和 df3
之间的重叠.
Ideally, the above code would be able to incorporate overlap between df1
, df2
and df3
.
推荐答案
您可以将数据框放在列表中.使用 intersect
为每个数据框生成一系列日期,并找到所有这三个日期中的共同日期.如果 df1
范围内存在 common_days
,则可以在 df1
中添加新列 Overlap
.>
You can put the dataframe in a list. Generate a sequence of days for each dataframe and find the common dates which are present in all 3 of them using intersect
. You can then add a new column Overlap
in df1
if the common_days
is present in range of df1
.
list_df <- list(df1, df2, df3)
common_days <- Reduce(intersect, lapply(list_df, function(x)
unlist(Map(':', x$date_start, x$date_end))))
df1$Overlap <- mapply(function(x, y) any(x:y %in% common_days),
df1$date_start, df1$date_end)
df1
# date_start date_end Site Variable Overlap
#1 2002-04-12 2002-04-21 aa Temp1 TRUE
#2 2002-06-26 2002-07-05 aa Temp1 FALSE
#3 2002-08-15 2002-08-20 aa Temp1 TRUE
#4 2005-08-08 2005-08-19 aa Temp1 TRUE
这篇关于确定三个(或更多)给定时间范围在R中是否重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!