Lazarus Mramba
2012-Mar-14 07:22 UTC
[R] Creating 250 submatrices from a large single matrix with 2500 variables using loops
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. Any help will be appreciated, Regards, Lazarus [[alternative HTML version deleted]]
Tsjerk Wassenaar
2012-Mar-14 08:29 UTC
[R] Creating 250 submatrices from a large single matrix with 2500 variables using loops
Hi Lazarus, Checkout arrays (?array). You can cast your matrix to an array of submatrices, and calculate the means per block using apply. Cheers, Tsjerk On Mar 14, 2012 9:25 AM, "Lazarus Mramba" <lmramba@gmail.com> 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. Any help will be appreciated, Regards, Lazarus [[alternative HTML version deleted]] ______________________________________________ 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. [[alternative HTML version deleted]]
Petr Savicky
2012-Mar-14 09:01 UTC
[R] Creating 250 submatrices from a large single matrix with 2500 variables using loops
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.
Tsjerk Wassenaar
2012-Mar-14 09:45 UTC
[R] Creating 250 submatrices from a large single matrix with 2500 variables using loops
Hi Lazarus, You should use apply, not sapply.> a =matrix(1:12, nrow=2,ncol=6,byrow=TRUE) > b = array(a,c(2,2,3));b > colMeans(b) > > f1=function(x) sum(x)/length(row(a.df[,1:2])) > y3 <- apply(b, 1, f1) > > It only gives one matrix with only two values, I expected 3 values.To apply the function on each submatrix, you have to apply it over the third index: y3 <- apply(b, 3, f1) Cheers, Tsjerk -- 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