MeanDims: Average an array along multiple dimensions

View source: R/MeanDims.R

MeanDimsR Documentation

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, drop = TRUE)

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

drop

A logical value indicating whether to keep the averaged dimension (FALSE) or drop it (TRUE). The default value is TRUE.

Value

A numeric or an array with the same dimension as parameter 'data' except the 'dims' dimensions. If 'drop' is TRUE, 'dims' will be removed; if 'drop' is FALSE, 'dims' will be preserved and the length will be 1. If all the dimensions are averaged out, a numeric is returned.

Examples

a <- array(rnorm(24), dim = c(dat = 2, member = 3, time = 4))
ens_mean <- MeanDims(a, 'member')
dim(ens_mean)
ens_time_mean <- MeanDims(a, c(2, 3), drop = FALSE)
dim(ens_time_mean)

s2dv documentation built on Oct. 13, 2024, 9:07 a.m.