Skip to contents

This family of functions performs pairwise set operations on two ivs. Pairwise refers to the fact that the i-th interval of x is going to be compared against the i-th interval of y. This is in contrast to their set-like counterparts (like iv_union()), which operate on the whole sets of x and y at once.

The descriptions of these operations are the same as their set-like counterparts, but the ones here also have a number of restrictions due to the fact that each must return an output that is the same size as its inputs:

  • For iv_pairwise_complement(), x[i] and y[i] can't overlap or abut, as this would generate an empty complement.

  • For iv_pairwise_union(), x[i] and y[i] can't be separated by a gap. Use iv_pairwise_span() if you want to force gaps to be filled anyways.

  • For iv_pairwise_intersect(), x[i] and y[i] must overlap, otherwise an empty interval would be generated.

  • For iv_pairwise_difference(), x[i] can't be completely contained within y[i], as that would generate an empty interval. Additionally, y[i] can't be completely contained within x[i], as that would result in two distinct intervals for a single observation.

  • For iv_pairwise_symmetric_difference(), x[i] and y[i] must share exactly one endpoint, otherwise an empty interval or two distinct intervals would be generated.

Usage

iv_pairwise_complement(x, y)

iv_pairwise_union(x, y)

iv_pairwise_span(x, y)

iv_pairwise_intersect(x, y)

iv_pairwise_difference(x, y)

iv_pairwise_symmetric_difference(x, y)

Arguments

x, y

[iv]

A pair of interval vectors.

These will be cast to the same type, and recycled against each other.

Value

An iv the same size and type as x and y.

See also

The set-like versions of these functions, such as iv_union().

Examples

x <- iv_pairs(c(1, 3), c(6, 8))
y <- iv_pairs(c(5, 7), c(2, 3))

iv_pairwise_complement(x, y)
#> <iv<double>[2]>
#> [1] [3, 5) [3, 6)

z <- iv_pairs(c(2, 5), c(4, 7))

iv_pairwise_union(x, z)
#> <iv<double>[2]>
#> [1] [1, 5) [4, 8)

# Can't take the union when there are gaps
try(iv_pairwise_union(x, y))
#> Error in iv_pairwise_union(x, y) : 
#>   Can't take the union of intervals containing a gap.
#>  Location 1 contains a gap.
#>  Use `iv_pairwise_span()` to combine across gaps.

# But you can force a union across gaps with `iv_pairwise_span()`
iv_pairwise_span(x, y)
#> <iv<double>[2]>
#> [1] [1, 7) [2, 8)

iv_pairwise_intersect(x, z)
#> <iv<double>[2]>
#> [1] [2, 3) [6, 7)

# Can't take an intersection of non-overlapping intervals
try(iv_pairwise_intersect(x, y))
#> Error in iv_pairwise_intersect(x, y) : 
#>   Can't take the intersection of non-overlapping intervals.
#>  This would result in an empty interval.
#>  Location 1 contains non-overlapping intervals.

iv_pairwise_difference(x, z)
#> <iv<double>[2]>
#> [1] [1, 2) [7, 8)

# The pairwise symmetric difference function is fairly strict,
# and is only well defined when exactly one of the interval endpoints match
w <- iv_pairs(c(1, 6), c(7, 8))
iv_pairwise_symmetric_difference(x, w)
#> <iv<double>[2]>
#> [1] [3, 6) [6, 7)