[R] calcualtign a trailing 12 column mean in a dataframe?

2006-03-29 Thread r user
I have a dataframe of 25 columns and 100,000 rows
called “testdf”.

I wish to build a new dataframe, with 14 columns and
100,000 rows.

I wish the new dataframe to have the “trailing 12
column” mean.  That is, I want column 1 of the new
dataframe to have soemthing like:

“( mean(testdf[,1:12],na.rm=T)”

What is the best way to accomplish this?

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] calcualtign a trailing 12 column mean in a dataframe?

2006-03-29 Thread Peter Ehlers
While we wait for others to provide the best way, here's
one way (if I understand your question correctly):

numrows - 10
x - rnorm(25 * numrows)
dat - as.data.frame(matrix(x, nc = 25))
newdat - as.data.frame(matrix(0, nr = numrows, nc = 14))
for(j in 1:14) newdat[, j] - rowMeans(dat[, j:(j + 11)], na.rm = TRUE)

Peter Ehlers

r user wrote:

 I have a dataframe of 25 columns and 100,000 rows
 called “testdf”.
 
 I wish to build a new dataframe, with 14 columns and
 100,000 rows.
 
 I wish the new dataframe to have the “trailing 12
 column” mean.  That is, I want column 1 of the new
 dataframe to have soemthing like:
 
 “( mean(testdf[,1:12],na.rm=T)”
 
 What is the best way to accomplish this?
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html