[R] binning a vector

Kevin Bartz kbartz at loyaltymatrix.com
Mon Jul 26 18:06:03 CEST 2004

Hi! For vector X, the vector to be averaged, along with binning vector Y,
you'd want to do something like this:

bpts  <- pretty(Y)
INDEX <- cut(Y, bpts, include.lowest = T)
tapply(X, INDEX, mean)

The idea is that R first breaks your binning vector Y into a factor, then
applies the "mean" function to X, over the levels of the factorized Y.
Incidentally, I've wrapped this in a "bapply" function included in a package
I'm working on:

bapply <- function(X, Y, FUN = NULL,
                   pretty.fn = pretty, pretty.arg = NULL,
                   cut.fn    = cut,    cut.arg    = NULL, ...) {
  bpts  <- do.call("pretty", c(list(Y), pretty.arg))
  INDEX <- do.call("cut", c(list(Y), cut.arg))
  tapply(X, INDEX, FUN, ...)

This allows you to apply the same way you do with "tapply." Let me know if
you have any questions.


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
Arne.Muller at aventis.com
Sent: Monday, July 26, 2004 8:12 AM
To: r-help at stat.math.ethz.ch
Subject: [R] binning a vector


I was wondering wether there's a function in R that takes two vectors (of
same length) as input and computes mean values for bins (intervals) or even
a sliding window over these vectros.

I've several x/y data set (input/response) that I'd like plot together. Say
the x-data for one data set goes from -5 to 14 with 12,000 values, then I'd
like to bin the x-vector in steps of +1 and calculate and plot the mean of
the x-values and the y-values within each bin.

I was browsing the R-docs but couldn't find anything appropiate.

	thanks for hints + kind regads,


R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list