R/fct_recode_if.R

Defines functions fct_recode_if

Documented in fct_recode_if

#' Recode a factor level on a condition
#'
#' @param f A factor variable
#' @param cond A predicate to determine when the level should be recoded
#' @param level The level created based on the condition
#'
#' @return A factor variable with the new level added in
#' @importFrom forcats fct_expand
#' @importFrom forcats fct_drop
#' @export
#'
#' @examples
#' df <- data.frame(x = factor(c("apple", "bear", "banana", "dear")),
#'                  y = factor(c("fruit", "animal", "fruit", "animal")))
#'
#' dplyr::mutate(df, x = fct_recode_if(x, y == "animal", "not a fruit"))

fct_recode_if <- function(f, cond, level) {
  f <- fct_expand(f, level)
  f[cond] <- level
  f <- fct_drop(f)
  f
}
philstraforelli/forcatsmisc documentation built on July 4, 2023, 9:52 a.m.