This family of functions counts different types of relationships between a
vector and an iv. It works similar to base::match()
, where needles[i]
checks for a match in all of haystack
.
iv_count_between()
counts instances of whenneedles
, a vector, falls between the bounds ofhaystack
, an iv.iv_count_includes()
counts instances of whenneedles
, an iv, includes the values ofhaystack
, a vector.
These functions return an integer vector the same size as needles
containing a count of the times where the i
-th value of needles
contained
a match in haystack
.
Usage
iv_count_between(needles, haystack, ..., missing = "equals", no_match = 0L)
iv_count_includes(needles, haystack, ..., missing = "equals", no_match = 0L)
Arguments
- needles, haystack
[vector, iv]
For
iv_*_between()
,needles
should be a vector andhaystack
should be an iv.For
iv_*_includes()
,needles
should be an iv andhaystack
should be a vector.Each element of
needles
represents the value / interval to match.haystack
represents the values / intervals to match against.
- ...
These dots are for future extensions and must be empty.
- missing
[integer(1) / "equals" / "error"]
Handling of missing values in
needles
."equals"
considers missing values inneedles
as exactly equal to missing values inhaystack
when determining if there is a matching relationship between them."error"
throws an error if any values inneedles
are missing.If a single integer value is provided, this represents the count returned for a missing value in
needles
. Use0L
to force missing values to never match.
- no_match
[integer(1) / "error"]
Handling of
needles
without a match."error"
throws an error if any needles have zero matches.If a single integer is provided, this represents the count returned for a needle with zero matches. The default value gives unmatched needles a count of
0L
.
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)
# Count the number of times `x` is between the intervals in `y`
iv_count_between(x, y)
#> [1] 1 1 2 0
# Count the number of times `y` includes a value from `x`
iv_count_includes(y, x)
#> [1] 0 2 1 1 0
# ---------------------------------------------------------------------------
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 values in `haystack`, so the missing value in `a` is
# considered between the missing interval in `b`.
iv_count_between(a, b)
#> [1] 0 2
iv_count_includes(b, a)
#> [1] 1 1
# If you'd like to propagate missing values, set `missing = NA`
iv_count_between(a, b, missing = NA)
#> [1] 0 NA
iv_count_includes(b, a, missing = NA)
#> [1] NA NA
# If you'd like missing values to be treated as unmatched, set
# `missing = 0L`
iv_count_between(a, b, missing = 0L)
#> [1] 0 0
iv_count_includes(b, a, missing = 0L)
#> [1] 0 0