Re: [R] Average every 4 columns

2016-11-12 Thread Jim Lemon
Hi Miluj, Perhaps you didn't get my previous email. Let your data frame be named "msdf": block_col_summ<-function(x,step,block_size,FUN="mean") { dimx<-dim(x) return_value<-NA start<-1 end<-start+block_size-1 block_count<-1 while(end <= dimx[2]) { return_value[block_count]<- do.call(FUN

Re: [R] Average every 4 columns

2016-11-09 Thread Jim Lemon
Thanks - it made me realize that I had reversed the column and row selection rowMeans(x[seq(1:dim(x)[1],by=4),-1]) Jim On Thu, Nov 10, 2016 at 8:30 AM, Uwe Ligges wrote: > > > On 09.11.2016 22:06, Jim Lemon wrote: >> >> Hi Milu, >> Perhaps this will help: >> >> apply(as.matrix(x[-1,seq(1:dim(x

Re: [R] Average every 4 columns

2016-11-09 Thread Uwe Ligges
On 09.11.2016 22:06, Jim Lemon wrote: Hi Milu, Perhaps this will help: apply(as.matrix(x[-1,seq(1:dim(x)[1],by=4)]),1,mean) More efficient than apply(..., 1, mean): rowMeans() Best, Uwe Ligges Jim On Thu, Nov 10, 2016 at 4:00 AM, Miluji Sb wrote: Thanks a lot for your quick reply.

Re: [R] Average every 4 columns

2016-11-09 Thread Jim Lemon
Hi Milu, Perhaps this will help: apply(as.matrix(x[-1,seq(1:dim(x)[1],by=4)]),1,mean) Jim On Thu, Nov 10, 2016 at 4:00 AM, Miluji Sb wrote: > Thanks a lot for your quick reply. I made a mistake in the question, I > meant to ask every 4 (or 12) rows not columns. Apologies. Thanks again! > > Sin

Re: [R] Average every 4 columns

2016-11-09 Thread David Winsemius
> On Nov 9, 2016, at 9:24 AM, David Winsemius wrote: > > >> On Nov 9, 2016, at 9:00 AM, Miluji Sb wrote: >> >> Thanks a lot for your quick reply. I made a mistake in the question, I >> meant to ask every 4 (or 12) rows not columns. Apologies. Thanks again! > > Then try matrix( sapply( split(

Re: [R] Average every 4 columns

2016-11-09 Thread David Winsemius
> On Nov 9, 2016, at 9:00 AM, Miluji Sb wrote: > > Thanks a lot for your quick reply. I made a mistake in the question, I > meant to ask every 4 (or 12) rows not columns. Apologies. Thanks again! Then try matrix( sapply( split( x , 0:(dim(x)[2]/4-1)*4+1] ), mean) Again assuming that dim(x)[1]

Re: [R] Average every 4 columns

2016-11-09 Thread Dalthorp, Daniel
Better is... rowMeans(x[0:((dim(x)[1]-1)/4)*4+1, ]) On Wed, Nov 9, 2016 at 9:00 AM, Miluji Sb wrote: > Thanks a lot for your quick reply. I made a mistake in the question, I > meant to ask every 4 (or 12) rows not columns. Apologies. Thanks again! > > Sincerely, > > Milu > > On Wed, Nov 9, 2016

Re: [R] Average every 4 columns

2016-11-09 Thread Dalthorp, Daniel
rowMeans(x[0:(dim(x)[1]/4-1)*4+1, ]) On Wed, Nov 9, 2016 at 9:00 AM, Miluji Sb wrote: > Thanks a lot for your quick reply. I made a mistake in the question, I > meant to ask every 4 (or 12) rows not columns. Apologies. Thanks again! > > Sincerely, > > Milu > > On Wed, Nov 9, 2016 at 5:45 PM, Dal

Re: [R] Average every 4 columns

2016-11-09 Thread Miluji Sb
Thanks a lot for your quick reply. I made a mistake in the question, I meant to ask every 4 (or 12) rows not columns. Apologies. Thanks again! Sincerely, Milu On Wed, Nov 9, 2016 at 5:45 PM, Dalthorp, Daniel wrote: > Hi Milu, > The following should work for an array x (provided dim(x)[2] is di

Re: [R] Average every 4 columns

2016-11-09 Thread Dalthorp, Daniel
Hi Milu, The following should work for an array x (provided dim(x)[2] is divisible by 4): colMeans(x[,0:(dim(x)[2]/4-1)*4+1]) -Dan On Wed, Nov 9, 2016 at 8:29 AM, Miluji Sb wrote: > Dear all, > > I have a dataset with hundreds of columns, I am only providing only 12 > columns. Is it possible t

[R] Average every 4 columns

2016-11-09 Thread Miluji Sb
Dear all, I have a dataset with hundreds of columns, I am only providing only 12 columns. Is it possible to take the mean of every four (or 12) columns (value601, value602, value603, value604 etc.in this case) and repeat for the hundreds of columns? Thank you. Sincerely, Milu structure(list(va