R/sample_group_levels.R

Defines functions sample_group_levels

Documented in sample_group_levels

#' Function to randomly sample a specified number of group levels and then return all cases in data that match the randomly chosen group levels.
#'
#' Say what this function does in greater detail if that helps.
#' @param data is the data
#' @param group is a variable you want to sample levels from
#' @param n is the number of levels you want to sample
#' @keywords organization efficiency
#' @export
#' @examples
#' sample_group_levels(mtcars, group = cyl, n = 2)

sample_group_levels <- function(data, group, n = 1){
  group <- dplyr::enquo(group)
  groups <- data %>% dplyr::pull(!!group) %>% unique()
  sample_group_levels <- sample(groups, n)
  data %>% dplyr::filter(!!group %in% sample_group_levels)
}
Kidapt/keda documentation built on Oct. 15, 2019, 9:01 p.m.