Hi: You need to give a function for rollapply() to apply :)
Here's my toy example: d <- as.data.frame(matrix(rpois(30, 5), nrow = 10)) library(zoo) d1 <- zoo(d) # uses row numbers as index # rolling means of 3 in each subseries (columns) > rollmean(d1, 3) V1 V2 V3 2 3.000000 5.333333 4.333333 3 3.333333 4.333333 4.000000 4 2.666667 3.666667 3.333333 5 4.333333 3.666667 3.333333 6 5.000000 5.000000 5.000000 7 4.666667 4.666667 4.000000 8 5.666667 3.666667 4.000000 9 5.000000 3.000000 2.666667 # create new function > cv <- function(x) sd(x)/mean(x) # use new function in rollapply(): > rollapply(d1, 3, FUN = cv) V1 V2 V3 2 0.3333333 0.1082532 0.5329387 3 0.1732051 0.4803845 0.6614378 4 0.2165064 0.5677271 0.4582576 5 0.7418193 0.5677271 0.4582576 6 0.6000000 0.3464102 0.4000000 7 0.7525467 0.4948717 0.6614378 8 0.8882158 0.5677271 0.6614378 9 1.0583005 0.3333333 0.2165064 This is a simplistic example, but it should get you started. HTH, Dennis On Fri, Oct 15, 2010 at 2:00 AM, David A. <dasol...@hotmail.com> wrote: > Hi Dennis and Dimitris, > > thanks for your answers. I am trying the rollmean() function and also the > rollapply() function because I also want to calculate CV for the values. > For this I created a co.var() function. I am having problems using them. > > >co.var<-function(x)( > +sd(x)/mean(x) > +) > > > > dim(mydata) > [1] 1710 244 > > >xxx<-rollmean(mydata,3,by=3) > > works fine and creates a vector which I will transform into a matrix. I > still have to find out how to store the output in my previously created > 570x244 0's matrix in an ordered way. > > but, since the examples in the help page says it´s the same, I tried > > > xxx<-rollapply(mydata,3,mean,by=3) > Error in UseMethod("rollapply") : > no applicable method for 'rollapply' applied to an object of class > "c('matrix', 'integer', 'numeric')" > > and, with my created function... > > > xxx<-rollapply(ord_raw_filt.df,3,FUN=co.var,by=3) > Error in UseMethod("rollapply") : > no applicable method for 'rollapply' applied to an object of class > "c('matrix', 'integer', 'numeric')" > > Can you help me with the error? > > Dave > > ------------------------------ > Date: Fri, 15 Oct 2010 00:45:08 -0700 > Subject: Re: [R] using apply function and storing output > From: djmu...@gmail.com > To: dasol...@hotmail.com > CC: r-help@r-project.org > > > Hi: > > Look into the rollmean() function in package zoo. > > HTH, > Dennis > > On Fri, Oct 15, 2010 at 12:34 AM, David A. <dasol...@hotmail.com> wrote: > > > Hi list, > > I have a 1710x244 matrix of numerical values and I would like to calculate > the mean of every group of three consecutive values per column to obtain a > new matrix of 570x244. I could get it done using a for loop but how can I > do that using apply functions? > In addition to this, do I have to initizalize a 570x244 matrix with 0's to > store the calculated values or can the output matrix be generated while > calculating the mean values? > > Cheers, > > Dave > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.