Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mean2

Fast averaging over subset of vector elements


Description

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

Usage

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

meanOver(...)

Arguments

x

A numeric or logical vector of length N.

idxs

A vector indicating subset of elements to operate over. If NULL, no subsetting is done.

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.

...

Not used.

Details

mean2(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. mean2(..., refine = FALSE) is almost twice as fast as mean2(..., refine = TRUE).

Value

Returns a numeric scalar.

Author(s)

Henrik Bengtsson

See Also

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

Examples

x <- 1:10
n <- length(x)

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

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

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

matrixStats

Functions that Apply to Rows and Columns of Matrices (and to Vectors)

v0.58.0
Artistic-2.0
Authors
Henrik Bengtsson [aut, cre, cph], Constantin Ahlmann-Eltze [ctb], Hector Corrada Bravo [ctb], Robert Gentleman [ctb], Jan Gleixner [ctb], Peter Hickey [ctb], Ola Hossjer [ctb], Harris Jaffee [ctb], Dongcan Jiang [ctb], Peter Langfelder [ctb], Brian Montgomery [ctb], Hugh Parsonage [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.