Skip to content
Prev 367349 / 398502 Next

How to use apply() to fill matrix by rows or columns?

C W
Dear R,

I wanted to simulate a 5 by 3 matrix which fills up by either rows or
columns?

I started with the following filling the matrix by rows,

dat <- matrix(NA, nrow=5, ncol = 3)

for(i in 1:5){

    dat[i, ] <- rnorm(3)

}

But, R is known for no loop drama. Any suggestions?

Thanks!