R: Fast averaging over subset of vector elements

mean2 {matrixStats} | R Documentation

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

[Package matrixStats version 0.55.0 Index]