[R] vector math: calculating a rolling 12 row product?

2006-02-28 Thread r user
I have a dataframe of numeric values with 30 “rows” and 7 “columns”. For each column, beginning at “row” 12 and down to “row” 30, I wish to calculate the “rolling 12 row product”. I.e., within each column, I wish to multiply all the values in row 1:12, 2:13,…19:30. I wish to save the results as

Re: [R] vector math: calculating a rolling 12 row product?

2006-02-28 Thread Chuck Cleland
How about applying cumprod to the columns and then subsetting the result? apply(mydata, 2, cumprod)[12:30,] ?cumprod r user wrote: I have a dataframe of numeric values with 30 “rows” and 7 “columns”. For each column, beginning at “row” 12 and down to “row” 30, I wish to calculate the

Re: [R] vector math: calculating a rolling 12 row product?

2006-02-28 Thread Gabor Grothendieck
Use as.matrix to convert your data frame to a matrix and suppose we have this test data as a matrix: mat - matrix(seq(30*7), 30, 7) Then try this: library(zoo) mat2 - coredata(rapply(zoo(mat), 12, prod)) See: library(zoo) vignette(zoo) and the various zoo help files for more

Re: [R] vector math: calculating a rolling 12 row product?

2006-02-28 Thread Chuck Cleland
Sorry, I don't think I gave what you asked for, but cumprod() may still help. Chuck Cleland wrote: How about applying cumprod to the columns and then subsetting the result? apply(mydata, 2, cumprod)[12:30,] ?cumprod r user wrote: I have a dataframe of numeric values with 30 “rows”