Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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?

like image 644
abcde123483 Avatar asked Dec 01 '22 03:12

abcde123483


1 Answers

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
like image 200
kohske Avatar answered Dec 04 '22 01:12

kohske