R/sample_n.R

Defines functions sample_n.data.frame sample_n

#' Sample rows from a data frame
#'
#' @param x a `data.frame`
#' @param ... passed on to methods
#' @export
#'
sample_n <- function(x, ...){
  UseMethod("sample_n")
}




#' @rdname sample_n
#' @inheritParams base::sample
#' @return a `data.frame` with `size` rows
#' @export
sample_n.data.frame <- function(
  x,
  size = nrow(x),
  replace = FALSE,
  prob = NULL,
  ...
){
  x[sample(seq_len(nrow(x)), size = size, replace = replace, prob = prob), ]
}
s-fleck/hammr documentation built on July 19, 2023, 9:20 p.m.