switch(EXPRESSION, ...)
switch
.
NULL
if no expression is
picked.
switch
evaluates
EXPRESSION
(the first argument).
If the value of
EXPRESSION
is of mode
"character"
,
the function looks among the names of
the remaining arguments for one that exactly (not partially) matches the
character string (only using the first element of the value).
If there is a match, the corresponding argument is evaluated and returned
as the value of
switch
; otherwise the first unnamed argument is evaluated
and returned.
In the case when
EXPRESSION
is character, some arguments associated with
a name can be omitted; if the value of
EXPRESSION
matches a name without
an argument, the next non-omitted argument is selected -- see the examples
section.
If the first argument is not character, it is coerced to integer;
if it is in the range
1
to
nargs()-1
, then the corresponding argument
from the
...
is evaluated and returned as the value of
switch
.
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)}) switch(as.character(answer), yes=, YES=1, no=, NO=2, 3)