ivs (said, “eye-vees”) is a package dedicated to working with intervals in a generic way. It introduces a new type, the interval vector, which is generally referred to as an iv. An iv is created from two parallel vectors representing the starts (inclusive) and ends (exclusive) of the intervals, like this:
library(ivs) # Interval vector of integers iv(1:5, 7:11) #> <iv<integer>> #>  [1, 7) [2, 8) [3, 9) [4, 10) [5, 11) # Interval vector of dates starts <- as.Date("2019-01-01") + 0:2 ends <- starts + c(2, 5, 10) iv(starts, ends) #> <iv<date>> #>  [2019-01-01, 2019-01-03) [2019-01-02, 2019-01-07) [2019-01-03, 2019-01-13)
There are a number of useful things you can do with these, including:
Determining how two ivs are related (i.e. does one precede, follow, or overlap the other?) with
Grouping / Merging overlapping intervals within a single iv with
Splitting an iv on its overlapping endpoints with
Applying set theoretical operations on two ivs, such as
Interval vectors are completely generic, meaning that you can create them from any comparable type that is supported by vctrs. This means that user defined S3 types work automatically, like
The best way to learn about ivs is by reading the Getting Started vignette!
Install the released version from CRAN with:
You can install the development version of ivs from GitHub with:
# install.packages("devtools") devtools::install_github("DavisVaughan/ivs")
This package was inspired by many sources!
IRanges is the closest equivalent, and inspired many of the function names seen here. It is mainly focused on integer intervals, and always uses closed intervals. It is also based on S4, and unfortunately that currently means it can’t be used as a column in a tibble with the current limitations in vctrs.
intervals is another R package that supports intervals. It supports integer/numeric intervals and allows for varying the endpoint bounds.
data.table contains a function named
foverlaps()for detecting overlaps (which was inspired by
Why numbering should start at 0 is a small white paper that describes why right-open intervals are often the best choice.