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?
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?
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)