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(1)
idx <- srs(3, 10)
idx
#> [1] 9 4 7
# Select from data frame
df <- data.frame(id = 1:10, x = rnorm(10))
df[idx, ]
#> id x
#> 9 9 0.763593461
#> 4 4 -1.539950042
#> 7 7 -0.005767173
# With replacement (can have repeats)
set.seed(2)
idx <- srs(5, 10, replace = TRUE)
idx
#> [1] 5 6 6 8 1
df[idx, ] # Some rows may appear twice
#> id x
#> 5 5 -0.9285670
#> 6 6 -0.2947204
#> 6.1 6 -0.2947204
#> 8 8 2.4046534
#> 1 1 1.3297993