Skip to contents

iv_span() computes the span of an iv. The span is a single interval which encompasses the entire range of the iv. It is similar to iv_groups(), if groups were also merged across gaps.

iv_span() is a summary function, like min() and max(), so it always returns a size 1 iv, even for empty ivs. The empty argument can be used to control what is returned in the empty case.

Usage

iv_span(x, ..., missing = "propagate", empty = "missing")

Arguments

x

[iv]

An interval vector.

...

These dots are for future extensions and must be empty.

missing

["propagate" / "drop" / "error" / iv(1)]

Handling of missing intervals in x.

  • "propagate" forces iv_span() to return a missing interval if any missing intervals are detected in x.

  • "drop" drops missing intervals before computing the span. If this results in an empty vector, then empty will be applied.

  • "error" throws an error if any missing intervals are detected.

  • If an iv of size 1 is supplied, then this is returned if any missing intervals are detected. It is cast to the type of x before returning.

empty

["missing" / "error" / iv(1)]

Handling of empty x vectors.

  • "missing" forces iv_span() to return a missing interval if x is empty.

  • "error" throws an error if x is empty.

  • If an iv of size 1 is supplied, then this is returned if x is empty. It is cast to the type of x before returning.

Details

iv_span() is currently limited by the fact that it calls min() and max() internally, which doesn't work for all vector types that ivs supports (mainly data frames). In the future, we hope to be able to leverage vctrs::vec_min() and vctrs::vec_max(), which don't exist yet.

Examples

x <- iv_pairs(c(1, 5), c(2, 6), c(9, 10))

# The span covers the full range of values seen in `x`
iv_span(x)
#> <iv<double>[1]>
#> [1] [1, 10)

# Compare against `iv_groups()`, which merges overlaps but doesn't merge
# across gaps
iv_groups(x)
#> <iv<double>[2]>
#> [1] [1, 6)  [9, 10)

x <- iv_pairs(c(1, 3), c(NA, NA), c(5, 6), c(NA, NA))

# Because `iv_span()` is a summary function, if any missing intervals are
# present then it returns a missing interval by default
iv_span(x)
#> <iv<double>[1]>
#> [1] [NA, NA)

# Further control this with `missing`
iv_span(x, missing = "drop")
#> <iv<double>[1]>
#> [1] [1, 6)
try(iv_span(x, missing = "error"))
#> Error in iv_span(x, missing = "error") : 
#>   `x` can't contain missing values.
iv_span(x, missing = iv(-1, 0))
#> <iv<double>[1]>
#> [1] [-1, 0)

x <- iv(double(), double())

# If `x` is empty, then by default a missing interval is returned
iv_span(x)
#> <iv<double>[1]>
#> [1] [NA, NA)

# Control this with `empty`
try(iv_span(x, empty = "error"))
#> Error in iv_span(x, empty = "error") : `x` can't be empty.
iv_span(x, empty = iv(-Inf, Inf))
#> <iv<double>[1]>
#> [1] [-Inf, Inf)

# `empty` kicks in if `missing = "drop"` is used and all elements were
# missing
x <- iv(c(NA, NA), c(NA, NA), ptype = double())
iv_span(x, missing = "drop", empty = iv(-Inf, Inf))
#> <iv<double>[1]>
#> [1] [-Inf, Inf)