relation-count {ivs} | R Documentation |
Count relationships between two ivs
Description
This family of functions counts different types of relationships between
two ivs. It works similar to base::match()
, where needles[i]
checks for
a relationship in all of haystack
.
-
iv_count_overlaps()
counts instances of a specifictype
of overlap between the two ivs. -
iv_count_precedes()
counts instances whenneedles[i]
precedes (i.e. comes before) any interval inhaystack
. -
iv_count_follows()
counts instances whenneedles[i]
follows (i.e. comes after) any interval inhaystack
.
These functions return an integer vector the same size as needles
containing a count of the times a particular relationship between the i
-th
interval of needles
and any interval of haystack
occurred.
Usage
iv_count_overlaps(
needles,
haystack,
...,
type = "any",
missing = "equals",
no_match = 0L
)
iv_count_precedes(
needles,
haystack,
...,
closest = FALSE,
missing = "equals",
no_match = 0L
)
iv_count_follows(
needles,
haystack,
...,
closest = FALSE,
missing = "equals",
no_match = 0L
)
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
|
closest |
Should only the closest relationship be returned? If |
Value
An integer vector the same size as needles
.
See Also
Examples
library(vctrs)
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
# Count the number of times `x` overlaps `y` at all
iv_count_overlaps(x, y)
# Count the number of times `y` is within an interval in `x`
iv_count_overlaps(y, x, type = "within")
# Count the number of times `x` precedes `y`
iv_count_precedes(x, y)
# ---------------------------------------------------------------------------
a <- iv(c(1, NA), c(2, NA))
b <- iv(c(NA, NA), c(NA, NA))
# Missing intervals are seen as exactly equal by default, so they are
# considered to overlap
iv_count_overlaps(a, b)
# If you'd like missing intervals to be treated as unmatched, set
# `missing = 0L`
iv_count_overlaps(a, b, missing = 0L)
# If you'd like to propagate missing intervals, set `missing = NA`
iv_count_overlaps(a, b, missing = NA)