Draws a simple random sample of n units from a population of N units.

srs(n, N, replace = FALSE)

Arguments

n

Sample size (number of units to select).

N

Population size.

replace

Logical. Sample with replacement? Default is FALSE.

Value

An integer vector of selected indices (1 to N). Length is n. With replace = TRUE, indices may repeat.

Details

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

See also

systematic() for systematic sampling, bernoulli() for Bernoulli sampling

Examples

# 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