Using replicate() for simulation
Use the replicate() function to simulate 100,000 trials of waiting for the first heads after flipping coins with 20% chance of heads. Plot a histogram of this simulation by calling qplot().
This exercise is part of the course
Foundations of Probability in R
Exercise instructions
- Use
replicate()to simulate 100,000 geometric trials. Copy and paste the expression given to you as your second argument toreplicate(). - Plot a histogram by calling
qplot()on thereplications.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Existing code for finding the first instance of heads
which(rbinom(100, 1, 0.2) == 1)[1]
# Replicate this 100,000 times using replicate()
replications <-
# Histogram the replications with qplot
qplot(___)