iv_between() detects when needles, a vector, falls between the
bounds of haystack, an iv. It works similar to base::%in%, where
needles[i] checks for a match in all of haystack.
This function returns a logical vector the same size as needles containing
TRUE if the value in needles is between any interval in haystack and
FALSE otherwise.
Arguments
- needles
[vector, iv]needlesshould be a vector andhaystackshould be an iv.needlesshould have the same type as the start/end components ofhaystack.Each element of
needlesrepresents the value to search for.haystackrepresents the intervals to search in.
- haystack
[vector, iv]needlesshould be a vector andhaystackshould be an iv.needlesshould have the same type as the start/end components ofhaystack.Each element of
needlesrepresents the value to search for.haystackrepresents the intervals to search in.
- ...
These dots are for future extensions and must be empty.
- missing
[logical(1) / "equals" / "error"]Handling of missing values in
needles."equals"considers missing values inneedlesas exactly equal to missing intervals inhaystackwhen determining if there is a matching relationship between them. Matched missing values inneedlesresult in aTRUEvalue in the result, and unmatched missing values result in aFALSEvalue."error"throws an error if any values inneedlesare missing.If a single logical value is provided, this represents the value returned in the result for values in
needlesthat are missing. You can force missing values to be unmatched by setting this toFALSE, and you can force them to be propagated by setting this toNA.
Examples
x <- as.Date(c("2019-01-05", "2019-01-10", "2019-01-07", "2019-01-20"))
y <- iv_pairs(
as.Date(c("2019-01-01", "2019-01-03")),
as.Date(c("2019-01-04", "2019-01-08")),
as.Date(c("2019-01-07", "2019-01-09")),
as.Date(c("2019-01-10", "2019-01-20")),
as.Date(c("2019-01-15", "2019-01-20"))
)
x
#> [1] "2019-01-05" "2019-01-10" "2019-01-07" "2019-01-20"
y
#> <iv<date>[5]>
#> [1] [2019-01-01, 2019-01-03) [2019-01-04, 2019-01-08) [2019-01-07, 2019-01-09)
#> [4] [2019-01-10, 2019-01-20) [2019-01-15, 2019-01-20)
# Detect if any location where `x` is between the intervals in `y`
iv_between(x, y)
#> [1] TRUE TRUE TRUE FALSE
# ---------------------------------------------------------------------------
a <- c(1, NA)
b <- iv(c(NA, NA), c(NA, NA))
# By default, missing values in `needles` are treated as being exactly
# equal to missing intervals in `haystack`, so the missing value in `a` is
# considered between the missing interval in `b`.
iv_between(a, b)
#> [1] FALSE TRUE
# If you'd like to propagate missing values, set `missing = NA`
iv_between(a, b, missing = NA)
#> [1] FALSE NA
# If you'd like missing values to be treated as unmatched, set
# `missing = FALSE`
iv_between(a, b, missing = FALSE)
#> [1] FALSE FALSE