| Line 1... |
Line 1... |
| 1 |
% File src/library/base/man/switch.Rd
|
1 |
% File src/library/base/man/switch.Rd
|
| 2 |
% Part of the R package, http://www.R-project.org
|
2 |
% Part of the R package, http://www.R-project.org
|
| 3 |
% Copyright 1995-2010 R Core Team
|
3 |
% Copyright 1995-2013 R Core Team
|
| 4 |
% Distributed under GPL 2 or later
|
4 |
% Distributed under GPL 2 or later
|
| 5 |
|
5 |
|
| 6 |
\name{switch}
|
6 |
\name{switch}
|
| 7 |
\alias{switch}
|
7 |
\alias{switch}
|
| 8 |
\title{Select One of a List of Alternatives}
|
8 |
\title{Select One of a List of Alternatives}
|
| Line 24... |
Line 24... |
| 24 |
\details{
|
24 |
\details{
|
| 25 |
\code{switch} works in two distinct ways depending whether the first
|
25 |
\code{switch} works in two distinct ways depending whether the first
|
| 26 |
argument evaluates to a character string or a number.
|
26 |
argument evaluates to a character string or a number.
|
| 27 |
|
27 |
|
| 28 |
If the value of \code{EXPR} is not a character string it is coerced to
|
28 |
If the value of \code{EXPR} is not a character string it is coerced to
|
| 29 |
integer. If this is between 1 and \code{nargs()-1} then the
|
29 |
integer. Note that this also happens for \code{\link{factor}}s, with
|
| 30 |
corresponding element of \code{\dots} is evaluated and the result
|
30 |
a warning, as typically the character level is meant. If the integer
|
| - |
|
31 |
is between 1 and \code{nargs()-1} then the corresponding element of
|
| 31 |
returned: thus if the first argument is \code{3} then the fourth
|
32 |
\code{\dots} is evaluated and the result returned: thus if the first
|
| 32 |
argument is evaluated and returned.
|
33 |
argument is \code{3} then the fourth argument is evaluated and
|
| - |
|
34 |
returned.
|
| 33 |
|
35 |
|
| 34 |
If \code{EXPR} evaluates to a character string then that string is
|
36 |
If \code{EXPR} evaluates to a character string then that string is
|
| 35 |
matched (exactly)to the names of the elements in \code{\dots}. If
|
37 |
matched (exactly) to the names of the elements in \code{\dots}. If
|
| 36 |
there is a match then that element is evaluated unless it is missing,
|
38 |
there is a match then that element is evaluated unless it is missing,
|
| 37 |
in which case the next non-missing element is evaluated, so for
|
39 |
in which case the next non-missing element is evaluated, so for
|
| 38 |
example \code{switch("cc", a = 1, cc =, cd =, d = 2)} evaluates to
|
40 |
example \code{switch("cc", a = 1, cc =, cd =, d = 2)} evaluates to
|
| 39 |
\code{2}. If there is more than one match, the first matching element
|
41 |
\code{2}. If there is more than one match, the first matching element
|
| 40 |
is used. In the case of no match, if there is a unnamed element of
|
42 |
is used. In the case of no match, if there is a unnamed element of
|
| Line 90... |
Line 92... |
| 90 |
for(ch in ccc)
|
92 |
for(ch in ccc)
|
| 91 |
cat(ch,":", switch(EXPR = ch, a = 1, b = 2:3), "\n")
|
93 |
cat(ch,":", switch(EXPR = ch, a = 1, b = 2:3), "\n")
|
| 92 |
for(ch in ccc)
|
94 |
for(ch in ccc)
|
| 93 |
cat(ch,":", switch(EXPR = ch, a =, A = 1, b = 2:3, "Otherwise: last"),"\n")
|
95 |
cat(ch,":", switch(EXPR = ch, a =, A = 1, b = 2:3, "Otherwise: last"),"\n")
|
| 94 |
|
96 |
|
| - |
|
97 |
## switch(f, *) with a factor f
|
| - |
|
98 |
ff <- gl(3,1, labels=LETTERS[3:1])
|
| - |
|
99 |
ff[1] # C
|
| - |
|
100 |
## so one might expect " is C" here, but
|
| - |
|
101 |
switch(ff[1], A = "I am A", B="Bb..", C=" is C")# -> "A"
|
| - |
|
102 |
## so we give a warning
|
| - |
|
103 |
|
| 95 |
## Numeric EXPR does not allow a default value to be specified
|
104 |
## Numeric EXPR does not allow a default value to be specified
|
| 96 |
## -- it is always NULL
|
105 |
## -- it is always NULL
|
| 97 |
for(i in c(-1:3, 9)) print(switch(i, 1, 2 , 3, 4))
|
106 |
for(i in c(-1:3, 9)) print(switch(i, 1, 2 , 3, 4))
|
| 98 |
|
107 |
|
| 99 |
## visibility
|
108 |
## visibility
|