N <- 1000
arr_p_True <- runif(N)
arr_simulated <- sapply(arr_p_True, function(p) {
sample(c(T, F), 1, prob = c(p, 1 - p))
})
arr_p_True
is what I want to get, but with very large N this is very inefficient. sample()
does not seem to be the right function to consider in this case, because it is vectorized over the probability of choosing each of the elements in x
, but not vectorized over the probability of choosing the first element in x
as required in my example.
I cannot find the right keyword for the purpose... I keep on being directed back to sample()
. Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire