switch
Evaluate One of Several Expressions

Description

Branches the evaluation, depending on the value of the first argument.

Usage

switch(EXPR, ...)

Arguments

EXPR an object that is either character or numeric when it is evaluated. This object is used to select which of the remaining arguments to evaluate. An error occurs if the object is of length zero.
... one of these arguments (at most) is selected, evaluated, and then returned.

Details

switch evaluates EXPR.
Value
the value of the selected expression, or NULL if no expression is selected.
See Also
if, ifelse, Syntax.
Examples
dist <- 'logistic'
switch(dist, cauchy=rcauchy(1), logistic=rlogis(1), norm=rnorm(1))
x <- 2
switch(x, 2+2, print("this one is chosen"), { y <- exp(5); z <- sin(2.1)})
cat('Do you feel lucky? '); answer <- readline()
switch(as.character(answer), y=, yes=, Yes=, YES=1, n=, no=, No=, NO=2, 3)
Package base version 6.1.4-13
Package Index