View source: R/extract_intervals.R
| extract_intervals | R Documentation |
Extract intervals from "mnirs" time series data, specifying interval start and end boundaries by time value, event label, lap number, or sample index.
extract_intervals(
data,
nirs_channels = NULL,
time_channel = NULL,
event_channel = NULL,
sample_rate = NULL,
group_intervals = c("distinct", "ensemble"),
group_channels = NULL,
start = NULL,
end = NULL,
span = list(c(-60, 60)),
zero_time = FALSE,
verbose = TRUE,
event_groups = deprecated()
)
Interval start and end boundaries are specified using helper functions,
or by passing raw values directly:
by_time()Time values in units of time_channel.
by_label()Strings to match in event_channel.
All matching occurrences are returned.
by_lap()Lap numbers to match in event_channel. Resolves to the
first sample of each lap for start, and the last lap sample for end
by_sample()Integer sample indices (row numbers).
Raw values supplied to start/end are auto-coerced:
Numeric -> by_time()
Character -> by_label(),
Explicit integer (e.g. 2L) -> by_lap().
Use by_sample() explicitly for sample indices.
start and end can use different specification types (e.g., start by
label, end by time). When lengths differ, the shorter is recycled.
Multiple specification types can be combined for a single boundary with
list() (e.g. start = list(by_time(30), by_label("go"))).
Resolved boundary times are concatenated in the order supplied. Combined
specifications must use the by_ helpers directly: raw values are ignored
with a warning.
span additively expands the time span window around interval boundaries.
A two-value vector expands the start and end, respectively:
span = c(-60, 60) expands the start earlier by 60, and the end
later by 60. For example,
start = by_time(30), end = by_time(60), span = c(-5, 10) returns an
interval of [25, 70].
A single numeric value is recycled according to the sign: span = -60
becomes c(-60, 0) to expand the start earlier. span = 60 becomes
c(0, 60) to expand the end later.
If only start is specified alone, both span values expand the single
boundary window: start = by_time(30), span = c(-5, 60) returns
[25, 90].
When group_intervals = "ensemble" or a list of numeric grouped intervals,
group_channels can be specified as a list of column names to override
which channels are ensemble-averaged within each group. For example, to
exclude a channel from one interval:
group_channels = list( c(A, B, C), c(A, C) ## channel "B" data are excluded from the second interval )
If all grouped intervals include all nirs_channels, group_channels can
be left as NULL (the default) and all channels are ensemble-averaged
within every group.
group_intervals controls whether extracted intervals are returned as
distinct data frames or ensemble-averaged.
"distinct"The default. Extract each interval and return a list of independent data frames.
"ensemble"Ensemble-average each specified nirs_channel across
all detected intervals and return a one-item list with a single data
frame.
list(c(1, 2), c(3, 4))Ensemble-average each specified
nirs_channel within each group and return a list with one data frame
for each group. Any intervals detected but not specified in
group_intervals are returned as distinct.
group_intervals lists can be named (e.g.
list(low = c(1, 2), high = c(3, 4))) and will pass those names to the
returned list of data frames.
When group_intervals is a list of numeric interval numbers, list items in
group_channels and span are recycled to the number of groups. If lists
are only partially specified, the final item is recycled forward as needed.
Extra items are ignored.
A named list() of tibbles of class
"mnirs", each with metadata available via attributes(). When data
is a list of data frames, results are flattened into a single-layer
list with interval names indicating nested number of data frame and
interval as interval_<df>.<interval>. Other interval names (e.g.
"ensemble" or custom group names) are suffixed as <name>_<df>.
## read example data
data <- read_mnirs(
example_mnirs("train.red"),
nirs_channels = c(
smo2_left = "SmO2 unfiltered",
smo2_right = "SmO2 unfiltered"
),
time_channel = c(time = "Timestamp (seconds passed)"),
zero_time = TRUE,
verbose = FALSE
) |>
## avoid issues ensemble-averaging irregular samples
resample_mnirs(method = "linear", verbose = FALSE)
## ensemble-average across multiple intervals
interval_list <- extract_intervals(
data, ## channels recycled to all intervals by default
nirs_channels = c(smo2_left, smo2_right),
group_intervals = "ensemble", ## ensemble-average across two intervals
start = by_time(368, 1084), ## manually identified interval start times
span = c(-20, 90), ## include the last 180-sec of each interval (recycled)
zero_time = TRUE ## re-calculate common time to start from `0`
)
interval_list[[1L]]
if (requireNamespace("ggplot2", quietly = TRUE)) {
plot(interval_list, time_labels = TRUE) +
ggplot2::geom_vline(xintercept = 0, linetype = "dotted")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.