Skip to content
Prev 365867 / 398502 Next

create list of vectors in for loop

On 06 Dec 2016, at 11:17 , Jim Lemon <drjimlemon at gmail.com> wrote:

            
As a principle, you want 

  list_of_vecs <- vector("list", 10)

to avoid extending the list on each iteration.


However, a simpler way is

replicate(10, rnorm(10), simplify=FALSE)

(where the simplify bit prevents conversion to 10x10 matrix)

or 

lapply(1:10, function(i) rnorm(10))