# [R] confused by switch() and the letter E

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 9 15:40:41 CET 2001

```On Fri, 9 Nov 2001, Andrew Bernat wrote:

> I can't get switch() to work as expected with the letter 'E'.  The following example from help  works just fine.
>
> > ccc <- c("b","QQ","a","A","bb")
> > for(ch in ccc) cat(ch,":",switch(ch, a=1,    b=2:3),            "\n")
> b : 2 3
> QQ :
> a : 1
> A :
> bb :
>
> Now I replace "a=1" with "E=1"
>
> > for(ch in ccc) cat(ch,":",switch(ch, E=1,    b=2:3),            "\n")
> b : b
> QQ : QQ
> a : a
> A : A
> bb : bb
>
> If instead I replace "b=2:3" with "E=2:3"  I get this:
>
> > for(ch in ccc) cat(ch,":",switch(ch, a=1,   E=2:3),            "\n")
> Error in switch(ch, a = 1, E = 2:3) : switch: EXPR must return a length 1 vector
> >
>
> "E" is the only letter that does this.  Even 'e' works fine.  Am I missing something obvious?  Thanks in advance for any help.   I'm using R 1.3.1 on Windows 2000.

Yes. The arguments to switch are switch(EXPR, ...).  If anything in ...
would partially match EXPR, you have to name the first argument, e.g.

switch(EXPR=ch, a= 1, E = 2:3)

It's often good practice to name arguments rather than rely on positional
matching, and it's a bad idea to abbreviate argument names in R code.

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```