R/sample.R

Defines functions sample_by

Documented in sample_by

#' 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)
}
paasim/stanutils documentation built on July 19, 2019, 12:47 a.m.