Flipping coins in R

Foundations of Probability in R

David Robinson

Chief Data Scientist, DataCamp

Flipping a coin

50% chance of heads

50% chance of tails

Foundations of Probability in R

Flipping a coin in R

rbinom(1, 1, .5)
# [1] 1

rbinom(1, 1, .5)
# [1] 0

Foundations of Probability in R

Flipping multiple coins

rbinom(10, 1, .5)
# [1] 0 1 1 0 1 1 1 0 1 0
rbinom(10, 1, .5)
# [1] 0 0 0 1 0 1 0 1 0 0
rbinom(1, 10, .5)
# [1] 4
rbinom(10, 10, .5)
# [1] 3 6 5 7 4 8 5 6 4 5
Foundations of Probability in R

Unfair coins

rbinom(10, 10, .8)
# [1]  6  7  9 10  7  7  8  9  9  8
rbinom(10, 10, .2)
# [1] 2 2 1 2 2 4 3 1 0 2
Foundations of Probability in R

Binomial distribution

$$X_{1\ldots n} \sim \text{Binomial}(\text{size}, p)$$

Foundations of Probability in R

Let's practice!

Foundations of Probability in R

Preparing Video For Download...