View source: R/resample_mnirs.R
| resample_mnirs | R Documentation |
Up- or down-sample an "mnirs" data frame to a new sample rate, filling new samples via nearest-neighbour matching or interpolation.
resample_mnirs(
data,
time_channel = NULL,
sample_rate = NULL,
resample_rate = sample_rate,
method = c("none", "linear", "locf"),
verbose = TRUE
)
data |
A data frame of class "mnirs" containing time series data and metadata. |
time_channel |
A character string naming the time or sample column.
Must match a column name in
|
sample_rate |
A numeric sample rate in Hz.
|
resample_rate |
An optional sample rate (Hz) for the output data
frame. If |
method |
A character string specifying how new samples are filled. Default is "none". Filling must be opted into explicitly (see Details):
|
verbose |
Logical. Default is |
This function uses replace_missing() (based on stats::approx()) to
interpolate across new samples in the resampled data range.
time_channel and sample_rate are retrieved automatically from data
of class "mnirs", if not defined explicitly.
Otherwise, sample_rate will be estimated from the values in time_channel.
However, this may return unexpected values, and it is safer to define
sample_rate explicitly or retrieve it from "mnirs" metadata.
When resample_rate is omitted, the output has the same sample_rate as
the input but with a regular, evenly-spaced time_channel. This is useful
for regularising data that contains missing or repeated samples without
changing the nominal rate.
Numeric columns are interpolated according to method (see
?replace_missing). Non-numeric columns (e.g. character event labels,
integer lap numbers) are always filled by last-observation-carried-forward,
regardless of method:
For method = "none", existing rows are matched to the nearest original
values of time_channel without interpolation or filling, meaning newly
created samples and any NAs in the original data are returned as NA.
When down-sampling, numeric columns use time-weighted averaging. Non-numeric columns use the first valid value in each output bin.
A tibble of class "mnirs". Metadata are
stored as attributes and can be accessed with attributes(data).
## read example data
data <- read_mnirs(
file_path = example_mnirs("moxy_ramp"),
nirs_channels = c(smo2 = "SmO2 Live"),
time_channel = c(time = "hh:mm:ss"),
verbose = TRUE
)
## note warning about irregular sampling
data
data_resampled <- resample_mnirs(
data, ## blank channels will be retrieved from metadata
resample_rate = 2, ## blank by default will resample to `sample_rate`
method = "linear", ## linear interpolation across resampled indices
verbose = TRUE
)
## note the altered `time` values resolving the above warning
data_resampled
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.