This family of functions detects different types of relationships between
two ivs pairwise, where pairwise means that the i-th interval of
x is compared against the i-th interval of y. This is in contrast to
iv_overlaps(), which works more like base::%in%.
iv_pairwise_overlaps()detects a specifictypeof overlap between the i-th interval ofxand the i-th interval ofy.iv_pairwise_precedes()detects if the i-th interval ofxprecedes (i.e. comes before) the i-th interval ofy.iv_pairwise_follows()detects if the i-th interval ofxfollows (i.e. comes after) the i-th interval ofy.
These functions return a logical vector the same size as the common size of
x and y.
Usage
iv_pairwise_overlaps(x, y, ..., type = "any")
iv_pairwise_precedes(x, y)
iv_pairwise_follows(x, y)Arguments
- x, y
[iv]A pair of interval vectors.
These will be recycled against each other and cast to the same type.
- ...
These dots are for future extensions and must be empty.
- type
[character(1)]The type of relationship to find. One of:
"any": Finds any overlap whatsoever between an interval inneedlesand an interval inhaystack."within": Finds when an interval inneedlesis completely within (or equal to) an interval inhaystack."contains": Finds when an interval inneedlescompletely contains (or equals) an interval inhaystack."equals": Finds when an interval inneedlesis exactly equal to an interval inhaystack."starts": Finds when the start of an interval inneedlesmatches the start of an interval inhaystack."ends": Finds when the end of an interval inneedlesmatches the end of an interval inhaystack.
Examples
library(vctrs)
x <- iv_pairs(
as.Date(c("2019-01-05", "2019-01-10")),
as.Date(c("2019-01-07", "2019-01-15")),
as.Date(c("2019-01-20", "2019-01-31"))
)
y <- iv_pairs(
as.Date(c("2019-01-01", "2019-01-03")),
as.Date(c("2019-01-07", "2019-01-09")),
as.Date(c("2019-01-18", "2019-01-21"))
)
x
#> <iv<date>[3]>
#> [1] [2019-01-05, 2019-01-10) [2019-01-07, 2019-01-15) [2019-01-20, 2019-01-31)
y
#> <iv<date>[3]>
#> [1] [2019-01-01, 2019-01-03) [2019-01-07, 2019-01-09) [2019-01-18, 2019-01-21)
# Does the i-th interval of `x` overlap the i-th interval of `y`?
iv_pairwise_overlaps(x, y)
#> [1] FALSE TRUE TRUE
# Does the i-th interval of `x` contain the i-th interval of `y`?
iv_pairwise_overlaps(x, y, type = "contains")
#> [1] FALSE TRUE FALSE
# Does the i-th interval of `x` follow the i-th interval of `y`?
iv_pairwise_follows(x, y)
#> [1] TRUE FALSE FALSE
a <- iv_pairs(c(1, 2), c(NA, NA), c(NA, NA))
b <- iv_pairs(c(NA, NA), c(3, 4), c(NA, NA))
# Missing intervals always propagate
iv_pairwise_overlaps(a, b)
#> [1] NA NA NA