I am looking for an implementation of union
for time intervals which is capable of dealing with unions that are not themselves intervals.
I have noticed lubridate
includes a union
function for time intervals but it always returns a single interval even if the union is not an interval (ie it returns the interval defined by the minimum of both start dates and the maximum of both end dates, ignoring intervening periods not covered by either interval):
library(lubridate)
int1 <- new_interval(ymd("2001-01-01"), ymd("2002-01-01"))
int2 <- new_interval(ymd("2003-06-01"), ymd("2004-01-01"))
union(int1, int2)
# Union includes intervening time between intervals.
# [1] 2001-01-01 UTC--2004-01-01 UTC
I have also looked at the interval
package, but its documentation makes no reference to union
.
My end goal is to use the complex union with %within%
:
my_int %within% Reduce(union, list_of_intervals)
So if we consider a concrete example, suppose the list_of_intervals
is:
[[1]] 2000-01-01 -- 2001-01-02
[[2]] 2001-01-01 -- 2004-01-02
[[3]] 2005-01-01 -- 2006-01-02
Then my_int <- 2001-01-01 -- 2004-01-01
is not %within%
the list_of_intervals
so it should return FALSE
and my_int <- 2003-01-01 -- 2006-01-01
is so it should be TRUE
.
However, I suspect the complex union has more uses than this.