1

I have a matrix of 30 rows:

set.seed(1234)
m = matrix(rnorm(300), 30)

I want to randomly divide it into three sub-matrix (each one will contain 10 rows) and calculate the colMeans of each sub-groups.

How I can do it?

d.b
  • 32,245
  • 6
  • 36
  • 77
  • 1
    Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to your questions, `set.seed(1234); matrix(rnorm(300), 30)` for example. And in most cases, adding the expected output is helpful. – lmo Nov 17 '17 at 01:50
  • 2
    `lapply(split(sample(1:30), ceiling(1:30/10)), function(i) colSums(m[i,]))` – d.b Nov 17 '17 at 01:56

1 Answers1

1

I would use sample to get the random grouping, rowsum to get the group column sums and then divide by 3 to get the mean.

Here,

rowsum(myMat, sample(rep(1:3, length.out=nrow(myMat)), nrow(myMat))) / 3

The length.out argument to rep assures that sample will have 30 elements to permute, 10 1s, 10 2s, and 10 3s. sample(rep(1:3, length.out=nrow(myMat)), nrow(myMat)) returns a random permutation of this set of integers that rowsums uses for the grouping.

The first five columns of this calculation returns

        [,1]       [,2]        [,3]      [,4]       [,5]
1 -1.5317639 -2.4137994  0.88905767 0.5783829  0.2593529
2 -0.6781386 -0.5701739 -0.03524136 0.3623384  1.4022571
3 -0.7543473 -2.5322095  0.53218802 0.1745588 -0.5748851

data

set.seed(1234)
matrix(rnorm(300), 30)
lmo
  • 37,904
  • 9
  • 56
  • 69