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.
replicate
is your friend. See ?replicate
replicate(10, my_function_call()) # this would be what you're looking for
链接地址: http://www.djcxy.com/p/24866.html
上一篇: 你如何在R中创建具有特定间隔的向量?
下一篇: 在R中创建重复函数调用结果的向量