[R] Help needed on "switch" function

Joris Meys jorismeys at gmail.com
Wed Jun 9 14:16:12 CEST 2010


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


More information about the R-help mailing list