Re: [R] Help needed on "switch" function

From: Joris Meys <jorismeys_at_gmail.com>
Date: Wed, 09 Jun 2010 14:16:12 +0200

PS : use a single = instead of a double. It's an assignment you do, not a comparison.
Cheers
Joris

On Wed, Jun 9, 2010 at 2:14 PM, Joris Meys <jorismeys_at_gmail.com> wrote:
> R is confused about the type in the switch, reads it as numeric
> instead of a character. Try :
>              temp1.name <- switch(temp1,
>                                  "1" = "aa",
>                                  "2" = "bb",
>                                  "3" = "cc",
>                                  "4" = "dd",
>                                  "5" = "ee")
>> temp1.name
> [1] "aa"
>
> cheers
> Joris
>
> On Wed, Jun 9, 2010 at 12:36 PM, Megh Dal <megh700004_at_yahoo.com> wrote:
>> Hi all, Here I am trying to implement the switch() function to choose value of a variable depending on the value of an input variable :
>>
>> temp1 <- "1"
>>               temp1.name <- switch(temp1,
>>                                   1 == "aa",
>>                                   2 == "bb",
>>                                   3 == "cc",
>>                                   4 == "dd",
>>                                   5 == "ee")
>>
>> Goal is if "temp1" equals to 1, then value of temp1.name would be "aa". However I am getting following answer :
>>
>>> temp1 <- "1"
>>>               temp1.name <- switch(temp1,
>> +                                   1 == "aa",
>> +                                   2 == "bb",
>> +                                   3 == "cc",
>> +                                   4 == "dd",
>> +                                   5 == "ee")
>>> temp1.name
>> [1] FALSE
>>
>>
>> Can anyone please point me where I am doing wrong?

>>
>> Thanks,
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
>
> --
> Joris Meys
> Statistical consultant
>
> Ghent University
> Faculty of Bioscience Engineering
> Department of Applied mathematics, biometrics and process control
>
> tel : +32 9 264 59 87
> Joris.Meys_at_Ugent.be
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>

-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys_at_Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 09 Jun 2010 - 12:18:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jun 2010 - 12:20:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive