rrandomsample

R sampling either 0 and 1 based on a vector of probabilities


I want to sample 10 numbers, either 0 or 1, with the following chances:

c(0.1989185, 0.2879522, 0.1142932, 0.3057067, 0.2056559, 0.1492126, 
0.2775737, 0.3332775, 0.9276861, 0.4373286)

The first number has a 0.1989185 chance of being 0, and the second has a 0.2879522 chance of being 0, etc.


Solution

  • Let x be your vector of probability, we can use:

    rbinom(length(x), 1, prob = 1 - x)
    

    Note: it is not prob = x, because your probability is for getting 0, not 1.