Learn R Programming

matrixStats (version 0.12.2)

binMeans: Fast mean calculations in non-overlapping bins

Description

Computes the sample means in non-overlapping bins

Usage

## S3 method for class 'default':
binMeans(y, x, bx, na.rm=TRUE, count=TRUE, right=FALSE, ...)

Arguments

y
A numeric vector of K values to calculate means on.
x
A numeric vector of K positions for to be binned.
bx
A numeric vector of B+1 ordered positions specifying the B bins [bx[1],bx[2]), [bx[2],bx[3]), ..., [bx[B],bx
na.rm
If TRUE, missing values in y are dropped before calculating the mean, otherwise not.
count
If TRUE, the number of data points in each bins is returned as attribute count, which is an integer
right
If TRUE, the bins are right-closed (left open), otherwise left-closed (right open).
...
Not used.

Value

Missing and non-finite values

Data points where either of y and x is missing are dropped (and therefore are also not counted). Non-finite values in y are not allowed and gives an error. Missing values in bx are not allowed and gives an error.

Empty bins

Empty bins will get value NaN.

Details

binMeans(x, bx, right=TRUE) gives equivalent results as rev(binMeans(-x, bx=sort(-bx), right=FALSE)), but is faster.

References

[1] R-devel thread Fastest non-overlapping binning mean function out there? on Oct 3, 2012

See Also

binCounts(). aggregate and mean().

Examples

Run this code
x <- 1:200
mu <- double(length(x))
mu[1:50] <- 5
mu[101:150] <- -5
y <- mu + rnorm(length(x))

# Binning
bx <- c(0,50,100,150,200)+0.5
yS <- binMeans(y, x=x, bx=bx)

plot(x,y)
for (kk in seq(along=yS)) {
  lines(bx[c(kk,kk+1)], yS[c(kk,kk)], col="blue", lwd=2)
}

Run the code above in your browser using DataLab