relation-locate {ivs} | R Documentation |
Locate relationships between two ivs
Description
This family of functions locates different types of relationships between
two ivs. It works similar to base::match()
, where needles[i]
checks for
a relationship in all of haystack
. Unlike match()
, all matching
relationships are returned, rather than just the first.
-
iv_locate_overlaps()
locates a specifictype
of overlap between the two ivs. -
iv_locate_precedes()
locates whereneedles[i]
precedes (i.e. comes before) any interval inhaystack
. -
iv_locate_follows()
locates whereneedles[i]
follows (i.e. comes after) any interval inhaystack
.
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 matching
relationship.
Usage
iv_locate_overlaps(
needles,
haystack,
...,
type = "any",
missing = "equals",
no_match = NA_integer_,
remaining = "drop",
multiple = "all",
relationship = "none"
)
iv_locate_precedes(
needles,
haystack,
...,
closest = FALSE,
missing = "equals",
no_match = NA_integer_,
remaining = "drop",
multiple = "all",
relationship = "none"
)
iv_locate_follows(
needles,
haystack,
...,
closest = FALSE,
missing = "equals",
no_match = NA_integer_,
remaining = "drop",
multiple = "all",
relationship = "none"
)
Arguments
needles , haystack |
Interval vectors used for relation matching.
Prior to comparison, |
... |
These dots are for future extensions and must be empty. |
type |
The type of relationship to find. One of:
|
missing |
Handling of missing intervals in
|
no_match |
Handling of
|
remaining |
Handling of
|
multiple |
Handling of
|
relationship |
Handling of the expected relationship between
|
closest |
Should only the closest relationship be returned? If |
Value
A data frame containing two integer columns named needles
and haystack
.
See Also
Detecting relationships pairwise
Locating relations from Allen's Interval Algebra
Examples
x <- iv_pairs(
as.Date(c("2019-01-05", "2019-01-10")),
as.Date(c("2019-01-07", "2019-01-15")),
as.Date(c("2019-01-20", "2019-01-31"))
)
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 overlap between `x` and `y`
loc <- iv_locate_overlaps(x, y)
loc
iv_align(x, y, locations = loc)
# Find where `x` contains `y` and drop results when there isn't a match
loc <- iv_locate_overlaps(x, y, type = "contains", no_match = "drop")
loc
iv_align(x, y, locations = loc)
# Find where `x` precedes `y`
loc <- iv_locate_precedes(x, y)
loc
iv_align(x, y, locations = loc)
# Filter down to find only the closest interval in `y` of all the intervals
# where `x` preceded it
loc <- iv_locate_precedes(x, y, closest = TRUE)
iv_align(x, y, locations = loc)
# Note that `closest` can result in duplicates if there is a tie.
# `2019-01-20` appears as an end date twice in `haystack`.
loc <- iv_locate_follows(x, y, closest = TRUE)
loc
iv_align(x, y, locations = loc)
# Force just one of the ties to be returned by using `multiple`.
# Here we just request any of the ties, with no guarantee on which one.
loc <- iv_locate_follows(x, y, closest = TRUE, multiple = "any")
loc
iv_align(x, y, locations = loc)
# ---------------------------------------------------------------------------
a <- iv(NA, NA)
b <- iv(c(NA, NA), c(NA, NA))
# By default, missing intervals in `needles` are seen as exactly equal to
# missing intervals in `haystack`, which means that they overlap
iv_locate_overlaps(a, b)
# If you'd like missing intervals in `needles` to always be considered
# unmatched, set `missing = NA`
iv_locate_overlaps(a, b, missing = NA)