I have defined a custom function, like this:
my.fun = function() {
for (i in 1:1000) {
...
for (j in 1:20) {
...
}
}
return(output)
}
which returns an output matrix, output
, composed by 1000 rows and 20 columns.
What I need to do is to repeat the function say 5 times and to store the five output
results into a brand new matrix, say final
, but without using another for-loop (this for making the code clearer, and also because in a second moment I would like to try to parallelize these additional 5 repetitions).
Hence final
should be a matrix with 5000 rows and 20 columns (the rationale behind these 5 repetitions is that within the two for-loops I use, among other functions, sample
).
I tried to use final <- replicate(5, my.fun())
, which correctly computes the five replications, but then I have to "manually" put the elements into a brand new 5000 x 20 matrix.. is there a more elgant way to do so? (maybe using sapply()
?). Many thanks