create_case_when: A case_when factory

Description Usage Arguments Details Value Methods (by generic) Examples

View source: R/create_case_when.R

Description

create_case_when allows to create reusable dplyr::case_when() functions. It returns a function that can be used in place of dplyr::case_when(). The arguments of the returned function are given by the vars argument of create_case_when().

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
create_case_when(..., vars = "x")

## S3 method for class 'case_when'
formulas(x, ...)

## S3 method for class 'case_when'
variable.names(object, ...)

## S3 method for class 'case_when'
print(x, ...)

Arguments

...

A sequence of two-sided formulas. The left hand side (LHS) determines which values match this case. The right hand side (RHS) provides the replacement value.

The LHS must evaluate to a logical vector. Each logical vector can either have length 1 or a common length. All RHSs must evaluate to the same type of vector.

These dots are evaluated with explicit splicing.

vars

A character vector that determined the names of the arguments of the returned function.

x, object

A case_when function, created by create_case_when.

Details

The returned function is of class case_when.

Value

A function, usable in place of dplyr::case_when().

Methods (by generic)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
x <- 1:50
y <- 51:100

cw_fb <- create_case_when(
  number %% 35 == 0 ~ "fizz buzz",
  number %% 5 == 0 ~ "fizz",
  number %% 7 == 0 ~ "buzz",
  TRUE ~ as.character(number),
  vars = "number"
)

cw_fb(number = x)
cw_fb(number = y)

# Formulas and variable names can be extracted
patterns <- formulas(cw_fb)
var_name <- variable.names(cw_fb)

# Dots support splicing
create_case_when(!!! patterns, vars = var_name)

RLesur/casewhen documentation built on May 5, 2019, 12:29 a.m.