Hello, I'm new to R and would like to know how to create a vector of "rolling sums". (I have seen the Rmetrics package and the rollMean function and I would like to do the same thing except Sum instead of Mean.) I imagine someone has done this, I just can't find it anywhere. Example: x <- somevector #where x is 'n' entries long #what I would like to do is: x1 <- x[1:20] output1 <- sum(x1) x2 <- x[2:21] output2 <- sum(x2) x3 <- ... ouput <- c(output1, output2, ...) Thanks, JV -- View this message in context: http://www.nabble.com/rolling-sum-%28like-in-Rmetrics-package%29-tp15459848p15459848.html Sent from the R help mailing list archive at Nabble.com.
Have you tried 'filter'?> x <- 1:20 > filter(x,filter=rep(1,5))Time Series: Start = 1 End = 20 Frequency = 1 [1] NA NA 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 NA NA>On 2/13/08, joshv <josh.verdone@yahoo.com> wrote:> > > Hello, I'm new to R and would like to know how to create a vector of > "rolling > sums". (I have seen the Rmetrics package and the rollMean function and I > would like to do the same thing except Sum instead of Mean.) I imagine > someone has done this, I just can't find it anywhere. > > Example: > x <- somevector #where x is 'n' entries long > > #what I would like to do is: > > x1 <- x[1:20] > output1 <- sum(x1) > > x2 <- x[2:21] > output2 <- sum(x2) > > x3 <- ... > > ouput <- c(output1, output2, ...) > > > Thanks, > JV > -- > View this message in context: > http://www.nabble.com/rolling-sum-%28like-in-Rmetrics-package%29-tp15459848p15459848.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
See ?filter e.g. output <- filter( x, rep(1,20) ) HTH, Chuck On Wed, 13 Feb 2008, joshv wrote:> > Hello, I'm new to R and would like to know how to create a vector of "rolling > sums". (I have seen the Rmetrics package and the rollMean function and I > would like to do the same thing except Sum instead of Mean.) I imagine > someone has done this, I just can't find it anywhere. > > Example: > x <- somevector #where x is 'n' entries long > > #what I would like to do is: > > x1 <- x[1:20] > output1 <- sum(x1) > > x2 <- x[2:21] > output2 <- sum(x2) > > x3 <- ... > > ouput <- c(output1, output2, ...) > > > Thanks, > JV > -- > View this message in context: http://www.nabble.com/rolling-sum-%28like-in-Rmetrics-package%29-tp15459848p15459848.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Hello, rollapply in zoo can probably help you : > x <- zoo( 1:10 ) > rollapply( x, 4, sum ) 2 3 4 5 6 7 8 10 14 18 22 26 30 34 > sum( x[1:4] ) [1] 10 > sum( x[2:5] ) [1] 14 Cheers, Romain -- Mango Solutions data analysis that delivers Introduction to R training course :: London :: 06-07 March 2008 http://www.mango-solutions.com/services/rtraining/r_intro.html joshv wrote:> Hello, I'm new to R and would like to know how to create a vector of "rolling > sums". (I have seen the Rmetrics package and the rollMean function and I > would like to do the same thing except Sum instead of Mean.) I imagine > someone has done this, I just can't find it anywhere. > > Example: > x <- somevector #where x is 'n' entries long > > #what I would like to do is: > > x1 <- x[1:20] > output1 <- sum(x1) > > x2 <- x[2:21] > output2 <- sum(x2) > > x3 <- ... > > ouput <- c(output1, output2, ...) > > > Thanks, > JV >
If there are no missing values then a rolling sum is just n * rolling mean so you can use your rolling mean. Also see: http://tolstoy.newcastle.edu.au/R/help/04/10/5161.html In the zoo package see rollapply and rollmean. In the caTools package see runmean and runsum.exact. runmean is particularly fast. On Feb 13, 2008 11:25 AM, joshv <josh.verdone at yahoo.com> wrote:> > Hello, I'm new to R and would like to know how to create a vector of "rolling > sums". (I have seen the Rmetrics package and the rollMean function and I > would like to do the same thing except Sum instead of Mean.) I imagine > someone has done this, I just can't find it anywhere. > > Example: > x <- somevector #where x is 'n' entries long > > #what I would like to do is: > > x1 <- x[1:20] > output1 <- sum(x1) > > x2 <- x[2:21] > output2 <- sum(x2) > > x3 <- ... > > ouput <- c(output1, output2, ...) > > > Thanks, > JV > -- > View this message in context: http://www.nabble.com/rolling-sum-%28like-in-Rmetrics-package%29-tp15459848p15459848.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >