switch2: Select one of a list of alternatives

View source: R/utils.R

switch2R Documentation

Select one of a list of alternatives

Description

switch which accepts a vector of strings.

Usage

switch2(x, ..., nomatch = NA)

Arguments

x

a vector of character strings

...

a list of alternatives; see switch

nomatch

the value returned when no matching case is given

Examples

switch('a',               a = 10, b = 2:0, c = 3, d = 1.5, e = mean)
switch('b',               a = 10, b = 2:0, c = 3, d = 1.5, e = mean)
# switch(c('a', 'b'),       a = 10, b = 2:0, c = 3, d = 1.5, e = mean) ## error

switch2(c('d', 'a'),      a = 10, b = 2:0, c = 3, d = 1.5, e = mean)
switch2(c('b', 'a'),      a = 10, b = 2:0, c = 3, d = 1.5, e = mean)
switch2(c('b', 'a', 'e'), a = 10, b = 2:0, c = 3, d = 1.5, e = mean)

## no matching expressions
switch2(c('a', 'b', 'e', 'f'), a = 1, b = 2)
switch2(c('a', 'b', 'e', 'f'), a = 1, b = 2, nomatch = 'other')


raredd/rawr documentation built on April 29, 2024, 10:29 a.m.