Dear all I am having a vector with large length and I would like to ask you if I can aggregate the values by constant sized windows. For example for the following vector, I would like to take 30 points until the end and find their mean.> myData<-seq(1:100000) > > c(mean(myData[1:30]),mean(myData[31:60])) #...and so one until the end[1] 15.5 45.5 I have searched in the R documentation and I found the aggregate but it seems to operate on data.frames. It also has this by argument where I tried to set it to 30 but it expects there a list rather than a numeric value. Could you please help me ? I would like to thank you in advance for your help B.R Alex [[alternative HTML version deleted]]
On Wed, Mar 14, 2012 at 07:46:40AM -0700, Alaios wrote:> Dear all I am having a vector with large length and I would like to ask you if I can aggregate the values by constant sized windows. For example for the following vector, I would like to take 30 points until the end > and find their mean. > > > > myData<-seq(1:100000) > > > > c(mean(myData[1:30]),mean(myData[31:60])) #...and so one until the end > [1] 15.5 45.5Hi. Try the following. myData <- 1:100 gr <- ceiling((1:length(myData))/30) c(tapply(myData, gr, FUN=mean)) 1 2 3 4 15.5 45.5 75.5 95.5 Hope this helps. Petr Savicky.
Hi> Dear all I am having a vector with large length and I would like to ask > you if I can aggregate the values by constant sized windows. For example> for the following vector, I would like to take 30 points until the end > and find their mean. > > > > myData<-seq(1:100000) > > > > c(mean(myData[1:30]),mean(myData[31:60])) #...and so one until the end > [1] 15.5 45.5 > > I have searched in the R documentation and I found the aggregate but it > seems to operate on data.frames. It also has this by argument where IWhy do you think so? Aggregate works on any suitable object, e.g. vector.> tried to set it to 30 but it expects there a list rather than a numericvalue.>From help pageby a list of grouping elements, each as long as the variables in x. the only thing what aggregate wants is list(some.vector.with.the.same.length.as.variable.you.want.to.aggregate) as some categorical data according to what you want to perform the function operation. Regards Petr> > Could you please help me ? > I would like to thank you in advance for your help > > B.R > Alex > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.