Draws a simple random sample of n units from a population of N units.
srs(n, N, replace = FALSE)An integer vector of selected indices (1 to N).
Length is n. With replace = TRUE, indices may repeat.
Without replacement (replace = FALSE):
Each possible sample of size n has equal probability
Inclusion probability: \(\pi_k = n/N\) for all units
n must not exceed N
With replacement (replace = TRUE):
Each draw is independent with probability 1/N per unit
Same unit can be selected multiple times
n can exceed N
systematic() for systematic sampling, bernoulli() for Bernoulli sampling
# Without replacement
set.seed(42)
idx <- srs(3, 10)
idx
#> [1] 1 5 10
# Select from data frame
df <- data.frame(id = 1:10, x = rnorm(10))
df[idx, ]
#> id x
#> 1 1 0.9559356
#> 5 5 0.5802063
#> 10 10 -1.0861326
# With replacement (can have repeats)
set.seed(42)
idx <- srs(5, 10, replace = TRUE)
idx
#> [1] 1 5 1 9 10
df[idx, ] # Some rows may appear twice
#> id x
#> 1 1 0.9559356
#> 5 5 0.5802063
#> 1.1 1 0.9559356
#> 9 9 0.1518129
#> 10 10 -1.0861326