iv-containers {ivs} | R Documentation |
Containers
Description
This family of functions revolves around computing interval containers. A container is defined as the widest interval that isn't contained by any other interval.
-
iv_containers()
returns all of the containers found withinx
. -
iv_identify_containers()
identifies the containers that each interval inx
falls in. It replacesx
with a list of the same size where each element of the list contains the containers that the corresponding interval inx
falls in. This is particularly useful alongsidetidyr::unnest()
. -
iv_identify_container()
is similar in spirit toiv_identify_containers()
, but is useful when you suspect that each interval inx
is contained within exactly 1 container. It replacesx
with an iv of the same size where each interval is the container that the corresponding interval inx
falls in. If any interval falls in more than one container, an error is thrown. -
iv_locate_containers()
returns a two column data frame with akey
column containing the result ofiv_containers()
and aloc
list-column containing integer vectors that map each interval inx
to the container that it falls in.
Usage
iv_containers(x)
iv_identify_containers(x)
iv_identify_container(x)
iv_locate_containers(x)
Arguments
x |
An interval vector. |
Value
For
iv_containers()
, an iv with the same type asx
.For
iv_identify_containers()
, a list-of containing ivs with the same size asx
.For
iv_identify_container()
, an iv with the same type asx
.For
iv_locate_containers()
, a two column data frame with akey
column containing the result ofiv_containers()
and aloc
list-column containing integer vectors.
Examples
library(dplyr, warn.conflicts = FALSE)
library(tidyr)
x <- iv_pairs(
c(4, 6),
c(1, 5),
c(2, 3),
c(NA, NA),
c(NA, NA),
c(9, 12),
c(9, 14)
)
x
# Containers are intervals which aren't contained in any other interval.
# They are always returned in ascending order.
# If any missing intervals are present, a single one is retained.
iv_containers(x)
# `iv_identify_container()` is useful alongside `group_by()` and
# `summarize()` if you know that each interval is contained within exactly
# 1 container
df <- tibble(x = x)
df <- mutate(df, container = iv_identify_container(x))
df
df %>%
group_by(container) %>%
summarize(n = n())
# If any interval is contained within multiple containers,
# then you can't use `iv_identify_container()`
y <- c(x, iv_pairs(c(0, 3), c(8, 13)))
y
try(iv_identify_container(y))
# Instead, use `iv_identify_containers()` to identify every container
# that each interval falls in
df <- tibble(y = y, container = iv_identify_containers(y))
df
# You can use `tidyr::unchop()` to see the containers that each interval
# falls in
df %>%
mutate(row = row_number(), .before = 1) %>%
unchop(container)
# A more programmatic interface to `iv_identify_containers()` is
# `iv_locate_containers()`, which returns the containers you get from
# `iv_containers()` alongside the locations in the input that they contain.
iv_locate_containers(y)