[R] create list of vectors in for loop

Paul Sanfilippo prseye at gmail.com
Tue Dec 6 10:44:03 CET 2016


Hi,

As an exercise, I am trying to create a list of 10 random number vectors in a loop. I can create the vectors but am unsure how to assemble them in the list as part of the loop. Any advice?


# Number of vectors to create
n <- c(1:10)

# Create empty list to store vectors
list_of_vecs <- list()

# Create n vectors of random numbers - length 10. This works ok.
for (i in seq_along(n)){
  assign(paste('vec_', i, sep = ''), rnorm(10,0,1))
}

# But how do I append them in a list. This doesn’t work:
for (i in seq_along(n)){
  list_of_vecs <- list(list_of_vecs,(assign(paste('vec_', i, sep = ''), rnorm(10,0,1))))
}


Thank you,

Paul



	[[alternative HTML version deleted]]



More information about the R-help mailing list