case_match: Vectorized 'switch()'

View source: R/case_match.R

case_matchR Documentation

Vectorized switch()

Description

Allows the user to succinctly create a new vector based off conditions of a single vector.

Usage

case_match(.x, ..., .default = NA, .ptype = NULL)

Arguments

.x

A vector

...

A sequence of two-sided formulas. The left hand side gives the old values, the right hand side gives the new value.

.default

The default value if all conditions evaluate to FALSE.

.ptype

Optional ptype to specify the output type.

Examples

df <- tidytable(x = c("a", "b", "c", "d"))

df %>%
  mutate(
    case_x = case_match(x,
                        c("a", "b") ~ "new_1",
                        "c" ~ "new_2",
                        .default = x)
  )

tidytable documentation built on Sept. 11, 2024, 8:05 p.m.