R/grep_case.R

Defines functions grep_case

Documented in grep_case

#' Switch-style recoding of values with string pattern matching
#'
#' @param x A vector
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> A sequence of two-sided
#'   formulas or named arguments.
#'
#'   - **Formulas**: Elements of `x` that match the [regex] pattern on the
#'   left hand side (LHS) of formulas will be replaced with the value in the
#'   right hand side (RHS).
#'   The LHS must evaluate to a character string.
#'   The RHS must be of length one.
#'   `NULL` inputs are ignored.
#'
#'   - **Named arguments**: named arguments are passed to [grepl()].
#'
#' @inheritParams switch_case
#'
#' @return A vector of the same length as `x`.
#'
#' @seealso [grep_case_fct()] to return a factor and
#'   [grep_case_list()] to return a list
#'
#'   [fn_case()], to apply a function other than `grepl()` to each case
#'
#'   [switch_case()] to recode values with exact matching
#'
#'   [in_case()], a pipeable alternative to [dplyr::case_when()]
#'
#'   [switch()] and [grepl()], which inspired this function
#'
#' @export
#'
#' @example examples/grep_case.R

grep_case <- function(x, ..., preserve = FALSE, default = NA) {
  fn_case(
    x  = x,
    fn = function(x, pattern, ...) grepl(pattern, x, ...),
    ...,
    preserve = preserve,
    default  = default
  )
}

Try the incase package in your browser

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

incase documentation built on Aug. 21, 2023, 9:09 a.m.