From P.Dalgaard at biostat.ku.dk  Mon Mar  9 15:21:32 2009
From: P.Dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Mon, 09 Mar 2009 15:21:32 +0100
Subject: [Rd] bug of *switch* function
In-Reply-To: <bc1181630903090655o1db1a397leb831a804c202761@mail.gmail.com>
References: <bc1181630903090655o1db1a397leb831a804c202761@mail.gmail.com>
Message-ID: <49B525EC.7070004@biostat.ku.dk>

guangchuang yu wrote:
> Hi,
> 
> When I call the *switch* function first time, it works. but when I call it
> at the second time, it does nothing. The version I use is R version 2.9.0
> Under development (unstable) (2009-02-21 r47969)
> 
> 
> here is the output:
> 
>> organism="human"
>>     species <- switch(organism,
>         human <- "Hs",
>         fly <- "Dm",
>         mouse <- "Mm",
>         rat <- "Rn",
>         yeast <- "Sc"
>     )
>  species <- switch(organism,
> +   human <- "Hs",
> +   fly <- "Dm",
> +   mouse <- "Mm",
> +   rat <- "Rn",
> +   yeast <- "Sc"
> +  )
>> species
> [1] "Hs"
>> organism="yeast"
>>     species <- switch(organism,
>         human <- "Hs",
>         fly <- "Dm",
>         mouse <- "Mm",
>         rat <- "Rn",
>         yeast <- "Sc"
>     )
>  species <- switch(organism,
> +   human <- "Hs",
> +   fly <- "Dm",
> +   mouse <- "Mm",
> +   rat <- "Rn",
> +   yeast <- "Sc"
> +  )
>> species
> [1] "Hs"
> 
> 
> when I change *organism* to "yeast", and call *switch* function again,
> *species* suppose to be changed to "Sc", but it remain it's original value.
> 
> You can see my screenshot at
> http://ygc.azpala.com/2009/03/09/bug-of-r-29-dev
> 

Wrong syntax. Check the examples of ?switch.

If the first argument to switch is not numeric, and does not match the
name of any named argument (of which you have none), then the result is
 the first unnamed argument (i.e. "Hs").

-- 
   O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



[Rd] bug of *switch* function
Peter Dalgaard