Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r generate random binary outcome with given probability

Tags:

r

I wish to generate random binary outcome with a given probability of 0.05. So in average I should get a positive (1) and 19 negatives (0) every 20 draws.

How can I do that with R ?

I see how I can generate with a 0.5 probability:

sample(0:1, 10000, replace=T)
mean(sample(0:1, 10000, replace=T))
[1] 0.5034

But I would need a probability of 0.05.

like image 290
Timothée HENRY Avatar asked Dec 01 '22 00:12

Timothée HENRY


1 Answers

There is a random generator for the binomial distribution whose purpose is exactly to generate such samples:

x <- rbinom(n=10000, size=1, prob=0.05)
like image 161
Vincent Guillemot Avatar answered Dec 05 '22 01:12

Vincent Guillemot