Suppose I write a function that takes no input but returns random variable e.g.,
example.f <- function() runif(1, 0, 1)
If I want to get a vector of length 100 of results returned from this function, I can't do this:
rep(example.f(), 100)
as it just repeats the first returned value. I could do it like this, with an anonymous function:
sapply(1:100, function(x) example.f())
but this strikes me as a bit inelegant. Is there another way?
Use replicate:
replicate(100, example.f())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With