I often face the problem that I want to draw samples repeatedly with replacement from c(0,1)
with varying vector of 'success' probabilities prob
, such as
prob <- c(1:5/10)
Two options using both a for
loop are:
A <- numeric()
n <- length(prob)
for(i in 1:n){
A[i] <- rbinom( 1 , 1 , prob = prob[i] )
}
and
for(i in 1:n){
A[i] <- sample( c(0,1) , 1 , prob = c(1-prob[i],prob[i]) )
}
Are there more straight forward (optimal, ellegant) ways to do this, e.g. without using the for
loop?