Learn R Programming

matrixStats (version 0.12.2)

meanOver: Fast averaging over subset of vector elements

Description

Computes the sample mean of all or a subset of values.

Usage

meanOver(x, idxs=NULL, na.rm=FALSE, refine=TRUE, ...)

Arguments

x
A numeric vector of length N.
idxs
A numeric index vector in [1,N] of elements to mean over. If NULL, all elements are considered.
na.rm
If TRUE, missing values are skipped, otherwise not.
refine
If TRUE and x is numeric, then extra effort is used to calculate the average with greater numerical precision, otherwise
...
Not used.

Value

Details

meanOver(x, idxs) gives equivalent results as mean(x[idxs]), but is faster and more memory efficient since it avoids the actual subsetting which requires copying of elements and garbage collection thereof.

If x is numeric and refine=TRUE, then a two-pass scan is used to calculate the average. The first scan calculates the total sum and divides by the number of (non-missing) values. In the second scan, this average is refined by adding the residuals towards the first average. The mean() uses this approach. meanOver(..., refine=FALSE) is almost twice as fast as meanOver(..., refine=TRUE).

See Also

mean(). To efficiently sum over a subset, see sumOver().

Examples

Run this code
x <- 1:10
n <- length(x)

idxs <- seq(from=1, to=n, by=2)
s1 <- mean(x[idxs])                        # 25
s2 <- meanOver(x, idxs=idxs)               # 25
stopifnot(identical(s1, s2))

idxs <- seq(from=n, to=1, by=-2)
s1 <- mean(x[idxs])                        # 25
s2 <- meanOver(x, idxs=idxs)               # 25
stopifnot(identical(s1, s2))

s1 <- mean(x)                              # 55
s2 <- meanOver(x)                          # 55
stopifnot(identical(s1, s2))

Run the code above in your browser using DataLab