From josh.m.ulrich at gmail.com  Mon Mar  9 15:46:31 2009
From: josh.m.ulrich at gmail.com (Josh Ulrich)
Date: Mon, 9 Mar 2009 09:46:31 -0500
Subject: [Rd] bug of *switch* function
In-Reply-To: <bc1181630903090735n4cafc4dak11c404394814ca4a@mail.gmail.com>
References: <bc1181630903090655o1db1a397leb831a804c202761@mail.gmail.com>
	<e8e755250903090731g66ba567fq63c37810f7c9b8d@mail.gmail.com>
	<bc1181630903090735n4cafc4dak11c404394814ca4a@mail.gmail.com>
Message-ID: <8cca69990903090746u7600e052l7244603c974616e6@mail.gmail.com>

Because "<-" assigs a value.  It does not name the alternatives.
Again, please read ?switch.

These two examples are the same:
> organism <- 'foo'
> (species <- switch(organism, human <- "HS", fly <- "DM", yeast <- "SC"))
[1] "HS"
> (species <- switch(organism, "HS", "DM", "SC"))
[1] "HS"


Josh
--
http://quantemplation.blogspot.com



On Mon, Mar 9, 2009 at 9:35 AM, guangchuang yu <guangchuangyu at gmail.com> wrote:
> But Why ?
>
> Why can not use "<-" ?
>
> On Mon, Mar 9, 2009 at 10:31 PM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
>
>> This isn't a bug in R.
>>
>> You are assigning within the switch statement, and it is returning the
>> first TRUE value (human).
>>
>> Use "=" not "<-"
>>
>> species <- switch(organism, human="HS", fly="DM", yeast="SC")
>> > species
>> [1] "SC"
>>
>>
>> HTH
>> Jeff
>>
>> On Mon, Mar 9, 2009 at 8:55 AM, guangchuang yu <guangchuangyu at gmail.com>
>> 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
>> >
>> > --
>> > Bests,
>> > Guangchuang Yu
>> >
>> > --~--~---------~--~----~------------~-------~--~----~
>> > Rm 848 Dept 9
>> > Institute of Radiation Medicine
>> > 27 Taiping Rd. Haidian Dist.
>> > Beijing, 100850, China
>> > Telephone: (86)-010-66931422
>> > Mobile: +86-13439009806
>> > Email: guangchuangyu at gmail.com
>> > -~----------~----~----~----~------~----~------~--~---
>> >
>> > ? ? ? ?[[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-devel at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-devel
>> >
>>
>>
>>
>> --
>> Jeffrey Ryan
>> jeffrey.ryan at insightalgo.com
>>
>> ia: insight algorithmics
>> www.insightalgo.com
>>
>
>
>
> --
> Bests,
> Guangchuang Yu
>
> --~--~---------~--~----~------------~-------~--~----~
> Rm 848 Dept 9
> Institute of Radiation Medicine
> 27 Taiping Rd. Haidian Dist.
> Beijing, 100850, China
> Telephone: (86)-010-66931422
> Mobile: +86-13439009806
> Email: guangchuangyu at gmail.com
> -~----------~----~----~----~------~----~------~--~---
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] bug of *switch* function
Josh Ulrich