Try this:
> library(zoo)
> t(rollapply(zoo(t(mydata)), 3, na.pad = TRUE, align = "right",
FUN = prod))
1 2 3 4 5 6
x.1 NA NA 861 52521 202581 499041
x.2 NA NA 1848 57288 213528 518568
x.3 NA NA 2967 62307 224847 538587
x.4 NA NA 4224 67584 236544 559104
x.5 NA NA 5625 73125 248625 580125
x.6 NA NA 7176 78936 261096 601656
x.7 NA NA 8883 85023 273963 623703
x.8 NA NA 10752 91392 287232 646272
x.9 NA NA 12789 98049 300909 669369
x.10 NA NA 15000 105000 315000 693000
x.11 NA NA 17391 112251 329511 717171
x.12 NA NA 19968 119808 344448 741888
x.13 NA NA 22737 127677 359817 767157
x.14 NA NA 25704 135864 375624 792984
x.15 NA NA 28875 144375 391875 819375
x.16 NA NA 32256 153216 408576 846336
x.17 NA NA 35853 162393 425733 873873
x.18 NA NA 39672 171912 443352 901992
x.19 NA NA 43719 181779 461439 930699
x.20 NA NA 48000 192000 480000 960000
On Fri, Apr 2, 2010 at 8:04 PM, Anna Stevenson
<anna.stevenson at rocketmail.com> wrote:> What is a "good" way to calculate the "moving product",
for each row of a dataframe, where I wish to be able to specify the length of
the moving product? Depending on my needs, I'd like?to be able to specify
the "length" over which to calculate the moving average (in this
example, length=3).
>
> For example, if I have a dataframe with 20 rows and 6 columns, and I wish
to calculate the moving product across a "length" of 3 columns.
>
>
>
> e.g.
> mydata <- data.frame(matrix(seq(20*6), 20, 6))
> mydata[1,]
>
> #? X1 X2 X3 X4 X5? X6
> #1? 1 21 41 61 81 101
>
> #I'd like to return a?dataframe that has the same dimensions as mydata.
> #e.g. This is what I'd want for the first row:
> #NA NA 861?52521?202581?499041
>
>
>
> ? ? ? ?[[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 guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>