Repeating the operation x times and capturing the results in a vector

If I have an operation involving randomness (lets call it operation_with_randomness(number)), and I want it to execute 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 (for example, not a loop structure) to run operation_with_randomness(number)each time to get a vector b that contains values ​​from x separate runs?

+3
source share
1 answer

Probably replication () 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
+14
source

Source: https://habr.com/ru/post/1792479/


All Articles