Re: [R] Summing over intervals

2010-07-15 Thread Gabor Grothendieck
On Thu, Jul 15, 2010 at 4:21 PM, Dennis Murphy wrote: > Hi: > > Tal was on the right track, but the function in package zoo that applies > here is rollapply(): > > library(zoo) > m<-matrix(seq(1,80),ncol=20, nrow=4) > t(apply(m, 1, function(x) rollapply(zoo(x), width = 5, FUN = mean, by = 5))) >  

Re: [R] Summing over intervals

2010-07-15 Thread Dennis Murphy
Hi: Tal was on the right track, but the function in package zoo that applies here is rollapply(): library(zoo) m<-matrix(seq(1,80),ncol=20, nrow=4) t(apply(m, 1, function(x) rollapply(zoo(x), width = 5, FUN = mean, by = 5))) [,1] [,2] [,3] [,4] [1,]9 29 49 69 [2,] 10 30 50

Re: [R] Summing over intervals

2010-07-15 Thread steven mosher
Eik and Patrick, Thanks I will give those a try. On Thu, Jul 15, 2010 at 8:15 AM, Patrick J Rogers wrote: > Hi Steven, > > You can just cut the matrix up into a 5 column matrix and use apply as > normal > > m2<-matrix(as.vector(t(m)), ncol=5, byrow=TRUE) > result<-matrix(apply(m2, 1, mean), nc

Re: [R] Summing over intervals

2010-07-15 Thread Patrick J Rogers
Hi Steven, You can just cut the matrix up into a 5 column matrix and use apply as normal m2<-matrix(as.vector(t(m)), ncol=5, byrow=TRUE) result<-matrix(apply(m2, 1, mean), ncol=ncol(m)/ncol(m2), byrow=TRUE) result -- Patrick Rogers Dept. of Political Science University of California, San Diego

Re: [R] Summing over intervals

2010-07-15 Thread Eik Vettorazzi
Hi Steven, how about this (using simple matrix algebra) wind<-5 #size of window to be aggregated if (ncol(m)%%wind!=0) stop('dimension of m does not fit') #crude check tmat<-apply(diag(ncol(m)/wind),1,function(x)rep(x,each=wind))/wind m%*%tmat hth. Am 15.07.2010 11:39, schrieb steven mosher: >

Re: [R] Summing over intervals

2010-07-15 Thread Tal Galili
You might find it useful to look at rollmean from the zoo package. Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-stati

[R] Summing over intervals

2010-07-15 Thread steven mosher
Given a matrix of MxN want to take the means of rows in the following fashion m<-matrix(seq(1,80),ncol=20, nrow=4) result<-matrix(NA,nrow=4,ncol=20/5) result[,1]<-apply(m[,1:5],1,mean) result[,2]<-apply(m[,6:10],1,mean) result[,3]<-apply(m[,11:15],1,mean) result[,4]<-apply(m[,16:20],1,mean)