R/shuffle.R

Defines functions fct_shuffle

Documented in fct_shuffle

#' Randomly permute factor levels
#'
#' @param f A factor (or character vector).
#' @export
#' @examples
#' f <- factor(c("a", "b", "c"))
#' fct_shuffle(f)
#' fct_shuffle(f)
fct_shuffle <- function(f) {
  f <- check_factor(f)

  lvls_reorder(f, sample(lvls_seq(f)))
}

Try the forcats package in your browser

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

forcats documentation built on Feb. 16, 2023, 8:57 p.m.