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

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat Feb 25 02:23:35 CET 2017

What is wrong with

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


And what is this "no loop drama" you refer to? I use loops frequently to loop around large memory gobbling chunks of code. 

Sent from my phone. Please excuse my brevity.

On February 24, 2017 5:02:46 PM PST, C W <tmrsg11 at gmail.com> wrote:
>Dear R,
>I wanted to simulate a 5 by 3 matrix which fills up by either rows or
>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?
>	[[alternative HTML version deleted]]
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list