R/other.R

Defines functions fct_other

Documented in fct_other

fct_other <- function(f, keep, drop, other_level = "Other") {
  f <- check_factor(f)

  if (!xor(missing(keep), missing(drop))) {
    stop("Must supply exactly one of `keep` and `drop`", call. = FALSE)
  }

  levels <- levels(f)
  if (!missing(keep)) {
    levels[!levels %in% keep] <- other_level
  } else {
    levels[levels %in% drop] <- other_level
  }

  f <- lvls_revalue(f, levels)
  fct_relevel(f, other_level, after = Inf)
}
sindribaldur/poorcats documentation built on Feb. 26, 2021, 12:27 a.m.