#' Subsample a df by groups
#'
#' Returns a subsample that contains `n` groups specified in `...`.
#'
#' @param df The df to be subsampled.
#' @param n The number of groups to be returned.
#' @param ... The groups.
#'
#' @export
sample_by <- function(df, n, ...) {
by <- enquos(...)
uniq <- distinct(df, !!!by)
sampled <- uniq[sample(nrow(uniq), n), ]
by_c <- intersect(colnames(df), colnames(sampled))
semi_join(df, sampled, by_c)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.