Мы можем использовать replicate
:
do.call(cbind,replicate(ncol/2, matrix(1:4, nrow = 2,ncol = 2),simplify = FALSE))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 1 3 1 3 1 3 1 3 1 3
#[2,] 2 4 2 4 2 4 2 4 2 4
Для использования for
l oop мы можем сделать:
ncol <- 10
m = matrix(, nrow = 2, ncol = ncol)
l <- matrix(1:4, nrow = 2, ncol = 2)
N <- ncol/2
for (i in (0:(N-1))) {
j <- i * 2
m[, c(j+1, j+2)] <- l
}