Re: [R] Computing row means for sets of 2 columns

2008-07-13 Thread Daren Tan
18:21:24 -0700> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> Subject: Re: [R] Computing row means for sets of 2 columns> CC: [EMAIL PROTECTED]> > m <- matrix(1:40, ncol=4);> groups <- rep(1:2, each=2);> uGroups <- unique(groups);> mMeans <- matrix(NA,

Re: [R] Computing row means for sets of 2 columns

2008-07-13 Thread Henrik Bengtsson
m <- matrix(1:40, ncol=4); groups <- rep(1:2, each=2); uGroups <- unique(groups); mMeans <- matrix(NA, nrow=nrow(m), ncol=length(uGroups)); for (gg in seq(along=uGroups)) { mMeans[,gg] <- rowMeans(m[,groups == uGroups[gg], drop=FALSE]); } (Preallocation of result matrix is more memory efficient

[R] Computing row means for sets of 2 columns

2008-07-13 Thread Daren Tan
Is there a better or more efficent approach than this without the use of t() ? > (m <- matrix(1:40, ncol=4)) [,1] [,2] [,3] [,4] [1,]1 11 21 31 > [2,]2 12 22 32 [3,]3 13 23 33 [4,]4 14 24 34 > [5,]5 15 25 35 [6,]6 16 26 36 [7,]