Last active
January 21, 2016 16:32
-
-
Save tjmahr/b656ac57eec75934850a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not robust against columns named with names wrapped in quotes like
df[, '"Subj"']