R/sample_n_by.R

Defines functions sample_n_by

Documented in sample_n_by

#' @include utilities.R
NULL
#' Sample n Rows By Group From a Table
#'
#' @description sample n rows by group from a table using the \code{\link[dplyr]{sample_n}()} function.
#'
#' @param data a data frame
#' @param ... Variables to group by
#' @param size the number of rows to select
#' @param replace with or without replacement?
#'
#' @examples
#' ToothGrowth %>% sample_n_by(dose, supp, size = 2)
#' @name sample_n_by
#' @export
sample_n_by <- function(data, ..., size = 1, replace = FALSE){
  data %>%
    group_by(...) %>%
    dplyr::sample_n(size = size, replace = replace) %>%
    dplyr::ungroup()
}

Try the rstatix package in your browser

Any scripts or data that you put into this service are public.

rstatix documentation built on Feb. 16, 2023, 6:10 p.m.