1 Let’s toss a coin
To illustrate the concepts behind object-oriented programming in R, we are going to consider a classic chance process (or chance experiment) of flipping a coin.
In this chapter you will learn how to implement code in R that simulates tossing a coin one or more times.
1.1 Coin object
Think about a standard coin with two sides: heads and tails.
To toss a coin using R, we first need an object that plays the role of a coin. How do you create such a coin? Perhaps the simplest way to create a coin with two sides,
"tails", is with a character vector via the combine function
You can also create a numeric coin that shows
0 instead of
Likewise, you can also create a logical coin that shows
1.2 Tossing a coin
Once you have an object that represents the coin, the next step involves learning how to simulate tossing the coin.
Tossing a coin is a random experiment: you either get heads or tails. One way to simulate the action of tossing a coin in R is with the function
sample() which lets you draw random samples, with or without replacement, of the elements of an input vector.
Here’s how to simulate a coin toss using
sample() to take a random sample of size 1 of the elements in
You use the argument
size = 1 to specify that you want to take a sample of size 1 from the input vector
1.2.1 Random Samples
sample() takes a sample of the specified
size without replacement. If
size = 1, it does not really matter whether the sample is done with or without replacement.
To draw two elements WITHOUT replacement, use
sample() like this:
What if you try to toss the coin three or four times?
Notice that R produced an error message. This is because the default behavior of
sample() cannot draw more elements than the length of the input vector.
To be able to draw more elements, you need to sample with replacement, which is done by specifying the argument
replace = TRUE, like this:
1.3 The Random Seed
sample() works is by taking a random sample from the input vector. This means that every time you invoke
sample() you will likely get a different output.
In order to make the examples replicable (so you can get the same output as mine), you need to specify what is called a random seed. This is done with the function
set.seed(). By setting a seed, every time you use one of the random generator functions, like
sample(), you will get the same values.
1.4 Sampling with different probabilities
Last but not least,
sample() comes with the argument
prob which allows you to provide specific probabilities for each element in the input vector.
prob = NULL, which means that every element has the same probability of being drawn. In the example of tossing a coin, the command
sample(coin) is equivalent to
sample(coin, prob = c(0.5, 0.5)). In the latter case we explicitly specify a probability of 50% chance of heads, and 50% chance of tails:
However, you can provide different probabilities for each of the elements in the input vector. For instance, to simulate a loaded coin with chance of heads 20%, and chance of tails 80%, set
prob = c(0.2, 0.8) like so:
1.4.1 Simulating tossing a coin
Now that we have all the elements to toss a coin with R, let’s simulate flipping a coin 100 times, and then use the function
table() to count the resulting number of
In my case, I got 56 heads and 44 tails. Your results will probably be different than mine. Sometimes you will get more
"heads", sometimes you will get more
"tails", and sometimes you will get exactly 50
"heads" and 50
Let’s run another series of 100 flips, and find the frequency of
"tails" with the help of the
To make things more interesting, let’s consider how the frequency of
heads evolves over a series of n tosses (in this case n =
With the vector
heads_freq, we can graph the (cumulative) relative frequencies with a line-plot:
Make a donation
If you find this resource useful, please consider making a one-time donation in any amount. Your support really matters.