Creating vector of results of repeated function calls in R

I have a function that uses runif to calculate some value, so each time it is called, the result varies slightly. I want to calculate the mean of the result of several calls to the function.

For this, it would be great to create a vector with the results of repeated function calls

Is there a simple idiomatic way to create a vector of repeated function calls? I tries

rep(my_function_call(), 10)

but it simply calls the function once and repeats the result 10 times. I want the function evaluated 10 times, and a vector of the results.


Solution 1:

replicate is your friend. See ?replicate

replicate(10, my_function_call()) # this would be what you're looking for