R/sample.R

Defines functions sample_n_if_needed

Documented in sample_n_if_needed

#' fill rows with sampled values (upsampling)
#' If the data frame has more than n rows, then sample through the data frame without replacement
#' If 
sample_n_if_needed <- function(tbl, size, fill) {
  if (nrow(tbl) >= size) {
    res <- dplyr::sample_n(tbl, size, replace = FALSE)
    return(res)
  }
  fill <- dplyr::select(fill, tidyselect::all_of(names(tbl)))
  res <- rbind(
    dplyr::sample_n(fill, size - nrow(tbl), replace = TRUE),
    tbl
  )
  dplyr::sample_n(res, size, replace = FALSE)
}
etiennebr/vertical-lidar-paper documentation built on Dec. 20, 2021, 6:42 a.m.