I have a function that creates a matrix and I want to call this function a thousand times such that in the end I have a list of 1000 matrices. Here is an example:
set.seed(1)
gen_mat <- function(x) matrix(c(1, 1, 1, x + rnorm(1)), nrow = 2)
Now, I tried replicate(10, gen_mat(1))
, but this returns an array and not a list. How to do it?
Combination of above answer, comment, and my own answer. Naturally, I like mine better. Also, I think there is a mistake in the above answer for
base
R.microbenchmarking the three methods
I used a larger value for
n
, but the results are similar in my desktop with smallern
as well. For this,replicate
takes longer than the other two methods.