[R] Creating 250 submatrices from a large single matrix with 2500 variables using loops

Petr Savicky savicky at cs.cas.cz
Wed Mar 14 10:01:08 CET 2012


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.



More information about the R-help mailing list