library(dplyr)
library(ggplot2)
library(mosaic)
Rewrite rflip(10)
using the resample()
command. Hint: coin <- c("H", "T")
Answer: resample()
by default samples with replacement and with equal probability. All we need to change is the size
argument:
coin <- c("H", "T")
# Compare the following multiple times:
resample(coin, size=10)
rflip(10)
Rewrite the shuffle()
command by changing the minimal number of default settings of resample()
. Test this on fruit
Answer: shuffle()
is the same as sampling without replacement. All you need to change the replace
argument from the default of TRUE
to FALSE
.
fruit <- c("apple", "orange", "mango")
# Compare the following multiple times:
resample(fruit, replace=FALSE)
shuffle(fruit)
Write code that will allow you to generate a sample of 15 fruit without replacement.
Answer: The following yields an error. You can’t sample more balls without replacement than there are balls in the machine. Alternatively, the largest sample without replacement of fruit
is of size 3.
fruit <- c("apple", "orange", "mango")
resample(fruit, size=15, replace=FALSE)
Write code that will allow you to generate a sample of 15 fruit with replacement.
Answer: This works!
fruit <- c("apple", "orange", "mango")
resample(fruit, size=15)
What’s the fastest way to do the above 5 times? Write it out
Answer: Use do()
!
fruit <- c("apple", "orange", "mango")
do(5) * resample(fruit, size=15)
## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
## 1 apple orange apple mango mango mango mango orange orange orange
## 2 orange apple apple orange orange apple orange apple apple apple
## 3 orange mango mango mango mango orange mango orange apple orange
## 4 orange orange orange orange apple orange apple apple mango orange
## 5 orange orange orange apple mango apple mango mango mango apple
## V11 V12 V13 V14 V15
## 1 mango apple apple orange mango
## 2 orange mango apple mango apple
## 3 mango orange apple mango orange
## 4 apple mango orange mango mango
## 5 orange orange mango mango orange