liveBook · Manning

Sample() function

As explained in Statistics Slam Dunk

The sample() function is a base R function that draws a sample or permutation from supplied values, optionally according to specified probabilities.

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

How Statistics Slam Dunk covers it

How deep it goes
Explains four arguments and demonstrates 10- and 700-draw simulations, but defers broader sampling cases.
Examples
Uses fair-coin sequences, 700 simulated flips, and Giannis Antetokounmpo free-throw streaks.
What it assumes
Assumes basic R vectors and function calls while teaching sampling arguments and independent trials from a coin-flip example.
What you can do after
Apply it via base R to simulate fair or weighted binary trials and investigate whether observed streaks exceed random expectations.
How it connects
Connects random sampling with probability, Bernoulli trials, regression to the mean, and the hot hand.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.