Description Usage Arguments Value Examples
View source: R/array_general.R
avgBin
computes averages of bins on a dimension of an array. The
size of bins or directly the bins themselves can be specified by the user.
1 2 3 4 5 6 7 8 |
dat |
matrix or array |
target_dim |
name of binned dimension of dat |
bin_length |
number of data points in one bin. If rolling parameter is FALSE (default), bin_length must match the length of the target dimension |
bin_ind |
an abbreviation for bin indices; a numeric or character vector or a factor which provides the bin membership for each data point of target_dim. If bin_ind is provided, bin_length is not used. |
rolling |
logical variable; if yes, avgBin computes rolling (or moving) window averages (the size of the moving window is determined by bin_length) |
newnames |
one of "avgs" (default), NULL, or a character vector defining the new dimension names of the binned dimension. If "avgs", the original names of target_dim are averaged per each bin. Not relevant if rolling is TRUE (which reuses the original labels). |
A matrix or array, depending on the input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # example dataset
data(erps)
# compute averages for each 10ms time bin (the sampling rate was 1000Hz);
# for present purposes, we can ignore the fact that 10ms binning results in
# data loss at the end of each time series
erps_bin <- avgBin(erps, "time", 10)
# compute rolling averages
erps_roll <- avgBin(erps, "time", 10, rolling = TRUE)
#
# compare some arbitrary time series
#
# create selection for the chosen conditions
sub <- list(stimclass = "A", pairtype = "ident", chan = "Cz", id = "01")
# extract time points
timepoints <- as.integer(dimnames(erps)$time)
timepoints_bin <- as.integer(dimnames(erps_bin)$time)
timepoints_roll <- as.integer(dimnames(erps_roll)$time)
stopifnot(identical(timepoints, timepoints_roll))
# plot series
plot(timepoints, subsetArray(erps, sub), type = "l")
lines(timepoints_bin, subsetArray(erps_bin, sub), col = 2)
lines(timepoints, subsetArray(erps_roll, sub), col = 3)
legend("topright", legend = c("original", "bin", "roll"), col = 1:3, lty = 1)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.