Skip to content

Commit

Permalink
Add documentation clarifying appropriate use of weights in dplyr's `s…
Browse files Browse the repository at this point in the history
…lice_sample()`.
  • Loading branch information
apeterson91 committed Jul 14, 2024
1 parent 0005f67 commit b69b8f5
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion R/slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@
#'
#' # you can optionally weight by a variable - this code weights by the
#' # physical weight of the cars, so heavy cars are more likely to get
#' # selected
#' # selected. Note that the weights cannot then be used to reconstruct
#' # summary statistics from the underlying population. See
#' # https://stats.stackexchange.com/q/639211/ for more details.
#' mtcars %>% slice_sample(weight_by = wt, n = 5)
#'
#' # Group wise operation ----------------------------------------
Expand Down Expand Up @@ -293,6 +295,9 @@ slice_max.data.frame <- function(.data, order_by, ..., n, prop, by = NULL, with_
#' @param weight_by <[`data-masking`][rlang::args_data_masking]> Sampling
#' weights. This must evaluate to a vector of non-negative numbers the same
#' length as the input. Weights are automatically standardised to sum to 1.
#' Note that these weights cannot be used to reconstruct summary statistics
#' via, for example, Horvitz-Thompson estimators. See
#' https://stats.stackexchange.com/q/639211/ for more details.
slice_sample <- function(.data, ..., n, prop, by = NULL, weight_by = NULL, replace = FALSE) {
check_dot_by_typo(...)
check_slice_unnamed_n_prop(..., n = n, prop = prop)
Expand Down

0 comments on commit b69b8f5

Please sign in to comment.