R/shuffle.R

Defines functions shuffle

Documented in shuffle

#' @title Safe Version of Sample
#'
#' @description
#' A version of `sample()` which does not treat positive scalar integer `x` differently.
#' See example.
#'
#' @param x (`vector()`)\cr
#'   Vector to sample elements from.
#' @param n (`integer()`)\cr
#'   Number of elements to sample.
#' @param ... (`any`)\cr
#'   Arguments passed down to [base::sample.int()].
#'
#' @export
#' @examples
#' x = 2:3
#' sample(x)
#' shuffle(x)
#'
#' x = 3
#' sample(x)
#' shuffle(x)
shuffle = function(x, n = length(x), ...) {
  x[sample.int(length(x), size = n, ...)]
}

Try the mlr3misc package in your browser

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

mlr3misc documentation built on Sept. 20, 2023, 5:06 p.m.