vector-locate {ivs} | R Documentation |
Locate relationships between a vector and an iv
Description
This family of functions locates 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
. Unlike match()
, all matches are
returned, rather than just the first.
-
iv_locate_between()
locates whereneedles
, a vector, falls between the bounds ofhaystack
, an iv. -
iv_locate_includes()
locates whereneedles
, an iv, includes the values ofhaystack
, a vector.
These functions return a two column data frame. The needles
column is an
integer vector pointing to locations in needles
. The haystack
column is
an integer vector pointing to locations in haystack
with a match.
Usage
iv_locate_between(
needles,
haystack,
...,
missing = "equals",
no_match = NA_integer_,
remaining = "drop",
multiple = "all",
relationship = "none"
)
iv_locate_includes(
needles,
haystack,
...,
missing = "equals",
no_match = NA_integer_,
remaining = "drop",
multiple = "all",
relationship = "none"
)
Arguments
needles , haystack |
For For
|
... |
These dots are for future extensions and must be empty. |
missing |
Handling of missing values in
|
no_match |
Handling of
|
remaining |
Handling of
|
multiple |
Handling of
|
relationship |
Handling of the expected relationship between
|
Value
A data frame containing two integer columns named needles
and haystack
.
See Also
Detect relationships between a vector and an iv
Pairwise detect relationships between a vector and an iv
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
y
# Find any location where `x` is between the intervals in `y`
loc <- iv_locate_between(x, y)
loc
iv_align(x, y, locations = loc)
# Find any location where `y` includes the values in `x`
loc <- iv_locate_includes(y, x)
loc
iv_align(y, x, locations = loc)
# Drop values in `x` without a match
loc <- iv_locate_between(x, y, no_match = "drop")
loc
iv_align(x, y, locations = loc)
# ---------------------------------------------------------------------------
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_locate_between(a, b)
iv_locate_includes(b, a)
# If you'd like missing values in `needles` to always be considered
# unmatched, set `missing = NA`
iv_locate_between(a, b, missing = NA)
iv_locate_includes(b, a, missing = NA)