avgBin: Compute averages of bins on a dimension of an array

Description Usage Arguments Value Examples

View source: R/array_general.R

Description

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.

Usage

1
2
3
4
5
6
7
8
avgBin(
  dat,
  target_dim,
  bin_length = NULL,
  bin_ind = NULL,
  rolling = FALSE,
  newnames = "avg"
)

Arguments

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).

Value

A matrix or array, depending on the input

Examples

 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)

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.