Learn R Programming

s2dv (version 0.1.1)

MeanDims: Average an array along multiple dimensions

Description

This function returns the mean of an array along a set of dimensions and preserves the dimension names if it has.

Usage

MeanDims(data, dims, na.rm = FALSE)

Arguments

data

An array to be averaged.

dims

A vector of numeric or charactor string, indicating along which dimensions to average.

na.rm

A logical value indicating whether to ignore NA values (TRUE) or not (FALSE). The default value is FALSE.

Value

An array with the same dimension as parameter 'data' except the 'dims' dimensions. removed.

Details

It is recommended to use 'apply(x, dim, mean)' to improve the efficiency when the dimension to be averaged is only one.

Examples

Run this code
# NOT RUN {
a <- array(rnorm(24), dim = c(a = 2, b= 3, c = 4))
print(dim(MeanDims(a, 2)))
print(dim(MeanDims(a, c(2, 3))))
print(dim(MeanDims(a, c('a', 'b'))))
# }

Run the code above in your browser using DataLab