Hello, 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, Arne
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. Kevin -----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 Hello, 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, Arne ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Arne.Muller at aventis.com wrote:> Hello, > > 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, > > Arne >Take a look at ?running or ?wapply in package:gregmisc. --sundar
On Monday 26 July 2004 10:11, Arne.Muller at aventis.com wrote:> Hello, > > 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.Do you know about loess? It's not exactly what you describe, but maybe it's what you really want. Deepayan
?tapply *********** REPLY SEPARATOR *********** On 7/26/2004 at 5:11 PM Arne.Muller at aventis.com wrote:>>>Hello, >>> >>>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, >>> >>> Arne >>> >>>______________________________________________ >>>R-help at stat.math.ethz.ch mailing list >>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlDipl. bio-chem. Eryk Witold Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: wolski at molgen.mpg.de ---W-W---- http://www.molgen.mpg.de/~wolski
Also, you can use cut and tapply to calculate means by bins. Andrew ----- Original Message ----- From: Deepayan Sarkar <deepayan at stat.wisc.edu> Date: Tuesday, July 27, 2004 3:38 am Subject: Re: [R] binning a vector> On Monday 26 July 2004 10:11, Arne.Muller at aventis.com wrote: > > Hello, > > > > 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. > > Do you know about loess? It's not exactly what you describe, but > maybe > it's what you really want. > > Deepayan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting- > guide.html