Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Last active January 21, 2016 16:32
Show Gist options
  • Select an option

  • Save tjmahr/b656ac57eec75934850a to your computer and use it in GitHub Desktop.

Select an option

Save tjmahr/b656ac57eec75934850a to your computer and use it in GitHub Desktop.
sample_n_groups <- function(df, n, group_keys = as.character(groups(df))) {
# Remember original grouping
group_keys_orig <- as.character(groups(df))
stopifnot(length(group_keys) != 0)
df_sub <- df %>%
# Keep just the grouping columns
group_by_(.dots = group_keys) %>%
select(one_of(group_keys)) %>%
distinct %>%
ungroup %>%
# Sample the grouping keys
sample_n(n, replace = FALSE)
# Keep just the sampled groups
df %>%
semi_join(df_sub, by = group_keys) %>%
# Restore original grouping
group_by_(.dots = group_keys_orig)
}
@tjmahr
Copy link
Copy Markdown
Author

tjmahr commented Jan 21, 2016

not robust against columns named with names wrapped in quotes like df[, '"Subj"']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment