Skip to contents

Create a small stratified sample from the given data

Usage

get_stratified_samp(
  .data,
  strata,
  n_per_stratum = NULL,
  replace = NULL,
  seed_val = NULL
)

Arguments

.data

A data frame or data frame extension (e.g. a tibble).

strata

A character vector of variables (column names) on which to stratify.

n_per_stratum

Sample size per stratum. Default: 3

replace

A boolean: whether to sample with replacement or not. Default: FALSE

seed_val

An integer for setting the RNG seed. Default: 123

Value

The stratified sample created from the given data frame.

Examples

df <- data.frame(
  group = rep(c("A", "B", "C"), each = 4),
  subgroup = rep(c("X", "Y"), times = 6),
  value = 1:24
)
df %>% get_stratified_samp(c("group", "subgroup"), n_per_stratum = 2)
#> # A tibble: 12 × 3
#>    group subgroup value
#>    <chr> <chr>    <int>
#>  1 A     X            1
#>  2 A     X           13
#>  3 A     Y           16
#>  4 A     Y           14
#>  5 B     X            5
#>  6 B     X            7
#>  7 B     Y           18
#>  8 B     Y           20
#>  9 C     X           21
#> 10 C     X           11
#> 11 C     Y           10
#> 12 C     Y           24

if (FALSE) { # \dontrun{
get_tus_dat() %>% get_stratified_samp()
} # }