?? Petr's solution is shorter and for large problems considerably faster...
-- Bert On Wed, Mar 14, 2012 at 2:08 AM, Tsjerk Wassenaar <tsje...@gmail.com> wrote: > Hi Lazarus, > > Sorry for my brevity before. I sent it from a phone. > > # Generate dummy data matrix > x <- matrix(1:100,nrow=5) > > # Rearrange into array; ten groups of two columns > y <- array(x,c(5,2,10)) > > # Average columns for each submatrix > apply(y,c(1,3),mean) > > # Average per submatrix > apply(y,3,mean) > > Hope it helps, > > Tsjerk > On Wed, Mar 14, 2012 at 10:01 AM, Petr Savicky <savi...@cs.cas.cz> wrote: >> On Wed, Mar 14, 2012 at 03:22:39AM -0400, Lazarus Mramba wrote: >>> Dear all, >>> >>> I have a large matrix with about 2500 variables, and 100 rows. >>> >>> I would like to calculate the means of the every 10 variables starting from >>> 1:2500 and saving the results as a vector or matrix. >>> How can I do that? >>> Alternatively, How can I create 250 subset matrices in the order of >>> variables 1:2500 in groups of 10 from the single matrix which had initially >>> 2500 variables ? >>> I guess I have to use a loop, but I can't figure out how. >> >> Hi. >> >> Try the following. I will use smaller parameters for simplicity. >> >> # a matrix 3 times 20 >> a <- matrix(1:60, nrow=3, ncol=20) >> a >> >> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] >> [,14] >> [1,] 1 4 7 10 13 16 19 22 25 28 31 34 37 >> 40 >> [2,] 2 5 8 11 14 17 20 23 26 29 32 35 38 >> 41 >> [3,] 3 6 9 12 15 18 21 24 27 30 33 36 39 >> 42 >> [,15] [,16] [,17] [,18] [,19] [,20] >> [1,] 43 46 49 52 55 58 >> [2,] 44 47 50 53 56 59 >> [3,] 45 48 51 54 57 60 >> >> #combine each 5 consecutive columns to a single column >> dim(a) <- c(15, 4) >> a >> >> [,1] [,2] [,3] [,4] >> [1,] 1 16 31 46 >> [2,] 2 17 32 47 >> [3,] 3 18 33 48 >> [4,] 4 19 34 49 >> [5,] 5 20 35 50 >> [6,] 6 21 36 51 >> [7,] 7 22 37 52 >> [8,] 8 23 38 53 >> [9,] 9 24 39 54 >> [10,] 10 25 40 55 >> [11,] 11 26 41 56 >> [12,] 12 27 42 57 >> [13,] 13 28 43 58 >> [14,] 14 29 44 59 >> [15,] 15 30 45 60 >> >> # compute column means >> colMeans(a) >> >> [1] 8 23 38 53 >> >> Hope this helps. >> >> Petr Savicky. >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > > > -- > Tsjerk A. Wassenaar, Ph.D. > > post-doctoral researcher > Molecular Dynamics Group > * Groningen Institute for Biomolecular Research and Biotechnology > * Zernike Institute for Advanced Materials > University of Groningen > The Netherlands > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.