sample() is a base R function for drawing values from a supplied vector. It can model real analytical questions about randomness, such as whether a basketball free-throw streak looks different from repeated coin flips. section 16.3
Control the draw with four main arguments:
- x supplies the possible values, such as c(0, 1) for heads and tails or missed and made free throws. section 16.3
- size sets how many values to draw, from a short demonstration of 10 flips to a 700-flip simulation. section 16.3 section 16.4.1
- prob assigns the probability of each possible value, such as c(0.5, 0.5) for a fair coin. section 16.3
- replace = TRUE allows independent trials, so a value can be drawn again on every flip. section 16.3
Simulating a free-throw-sized sequence
coin_flips <- sample(x = c(0, 1), size = 700, prob = c(0.5, 0.5), replace = TRUE) The result can be converted to a data frame, tracked with streak_run(), filtered to made shots, and summarized with max(). One simulation produced a maximum streak of 11 tails—comparable to a basketball free-throw streak—without requiring a hot-hand explanation. Change prob to c(0.7, 0.3) to simulate unequal odds. section 16.4.1 section 16.6
sample(x = c(0, 1), size = 10, prob = c(0.5, 0.5), replace = TRUE) ## [1] 0 1 1 1 1 1 1 0 1 0
Run the compact fair-coin example and inspect the returned sequence of zeros and ones. section 16.3
