in_case_list: Case statements returning a list

View source: R/in_case_list.R

in_case_listR Documentation

Case statements returning a list

Description

These functions are equivalent to in_case(), switch_case(), grep_case(), fn_case(), and fn_switch_case() but return lists.

Usage

in_case_list(..., preserve = FALSE, default = NA)

switch_case_list(x, ..., preserve = FALSE, default = NA)

grep_case_list(x, ..., preserve = FALSE, default = NA)

fn_case_list(x, fn, ..., preserve = FALSE, default = NA)

fn_switch_case_list(x, fn, ..., preserve = FALSE, default = NA)

Arguments

...

<dynamic-dots> A sequence of two-sided formulas or named arguments.

  • Formulas: Elements of x that return TRUE when passed to fn with the left hand side (LHS) of each formula will be replaced with the value in the right hand side (RHS). The LHS must evaluate to a logical vector when passed to fn with x. The RHS must be of length 1 or the same length as all other RHS.

  • Named arguments: named arguments are passed as additional arguments to the function fn.

preserve

If TRUE, unmatched elements of x will be returned unmodified. (The elements may have their type coerced to be compatible with replacement values.) If FALSE, unmatched elements of x will be replaced with default. Defaults to FALSE.

default

If preserve is FALSE, a value to replace unmatched elements of x. Defaults to NA.

x

A vector

fn

A function to apply to the left-hand side of each formula in ...

Either a quoted or unquoted function name, an anonymous function, or a purrr-style formula.

The function should take two inputs, the first being x and the second being the left-hand side of the formula. The function should return a logical vector, either of length 1 or the same length as x.

Details

This can be useful when returning a non-atomic value and/or when you want to create a list column inside a tibble.

Value

A list of length 1 or n, matching the length of the logical input vector.

See Also

in_case(), switch_case(), grep_case(), fn_case(), and fn_case_fct() on which these functions are based.

Examples

1:3 %>%
  in_case_list(
    . < 2   ~ mtcars,
    default = letters
  )

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