Skip to contents

Proportional or fixed stratified random sample

Usage

morie_stratified_sample(
  df,
  strata_col,
  n_per_stratum,
  proportional = FALSE,
  seed = 42L
)

Arguments

df

A data frame.

strata_col

Name of the stratification column.

n_per_stratum

Either an integer (equal allocation) or a named integer vector mapping stratum levels to sample sizes. If proportional = TRUE, n_per_stratum is treated as the total desired sample size and allocation is proportional to stratum size.

proportional

Logical; if TRUE, allocate proportionally to strata sizes.

seed

Random seed.

Value

Data frame of sampled rows with a .weight column.

Examples

df <- data.frame(g = c(rep("A", 60), rep("B", 40)), x = rnorm(100))
morie_stratified_sample(df, "g", n_per_stratum = 10)
#>    g           x .weight
#> 49 A  0.09734049       6
#> 37 A  2.45959355       6
#> 1  A -1.09615624       6
#> 25 A -0.39654974       6
#> 10 A -0.38328396       6
#> 36 A  0.34801230       6
#> 18 A -0.19081647       6
#> 60 A -0.03348752       6
#> 47 A -0.16626149       6
#> 24 A  0.43156537       6
#> 67 B -0.78488781       4
#> 96 B  0.27545697       4
#> 85 B  0.98859685       4
#> 97 B  1.15734707       4
#> 80 B -0.06876365       4
#> 86 B -0.07345833       4
#> 63 B -1.03435936       4
#> 98 B -1.68248086       4
#> 87 B -1.38702655       4
#> 64 B -0.63073195       4