Skip to content

Instantly share code, notes, and snippets.

@mrdwab
mrdwab / stratified.R
Last active Sep 9, 2021
Stratified random sampling from a `data.frame` in R
View stratified.R
stratified <- function(df, group, size, select = NULL,
replace = FALSE, bothSets = FALSE) {
if (is.null(select)) {
df <- df
} else {
if (is.null(names(select))) stop("'select' must be a named list")
if (!all(names(select) %in% names(df)))
stop("Please verify your 'select' argument")
temp <- sapply(names(select),
function(x) df[[x]] %in% select[[x]])