Search code examples
rrandomvectorizationprobability

How to sample according to a long array of probabilities of being True in R?


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.


Solution

  • You could generate a vector of random numbers from the unit interval. With probability p the value will be smaler than p:

    N <- 1000
    arr_p_True <- runif(N)
    arr_simulated <- runif(N) < arr_p_True