Reputation: 956
I'm trying to sample 10 values of either 0 or 1, with defined (but different) probabilities each time.
This does not work:
sample(0:1, size = 10, replace = T, prob = seq(0.1, 1, 0.1))
This does work:
sapply(seq(0.1, 1, 0.1), function(x) sample(0:1, size = 1, prob = c(1-x, x)))
The 10th value is always 1, while the first value has a 1/10 chance of being a 1 - which is what I want to achieve.
Is there a vectorized approach?
Upvotes: 0
Views: 51
Reputation: 132706
You want to sample from a binomial distribution:
rbinom(10, 1, prob = seq(0.1, 1, 0.1))
Upvotes: 3