Skip to contents

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 specific type of overlap between the i-th interval of x and the i-th interval of y.

  • iv_pairwise_precedes() detects if the i-th interval of x precedes (i.e. comes before) the i-th interval of y.

  • iv_pairwise_follows() detects if the i-th interval of x follows (i.e. comes after) the i-th interval of y.

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 in needles and an interval in haystack.

  • "within": Finds when an interval in needles is completely within (or equal to) an interval in haystack.

  • "contains": Finds when an interval in needles completely contains (or equals) an interval in haystack.

  • "equals": Finds when an interval in needles is exactly equal to an interval in haystack.

  • "starts": Finds when the start of an interval in needles matches the start of an interval in haystack.

  • "ends": Finds when the end of an interval in needles matches the end of an interval in haystack.

Value

A logical vector the same size as the common size of x and y.

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