开发者

Repeating an operation x times and catch the results in a vector

If I have an operation including randomness (lets call it operation_with_randomness(number)) and I want it to run x times

> 开发者_如何转开发b <- rep(operation_with_randomness(number), x)

I get

> b 
1.5472491 1.5472491 1.5472491 1.5472491 1.5472491 1.5472491 ...

Is there any smart way (e.g. not a loop structure) to run the operation_with_randomness(number) each time to get a b vector which holds values from x separate runs?


Probably replicate() will meet:

> z <- replicate(10, runif(1))
> z
 [1] 0.762778299 0.541601960 0.654238258 0.026323048 0.532011084 0.905059722 0.328891040 0.297307167 0.004157573 0.507231966
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜