Re: [R] How to use conditional statement

From: Ivan Calandra <ivan.calandra_at_uni-hamburg.de>
Date: Thu, 10 Mar 2011 13:32:36 +0100

Thanks for the comment, I didn't think about this thorougly.

Le 3/10/2011 12:54, Duncan Murdoch a écrit :
> On 11-03-10 5:54 AM, Ivan Calandra wrote:
>> Try with double "== "instead:
>> ifelse(val3 == "Monthly", val<- val1, val<- val2)
>
> That might work, but it is not how you should do it. (It should work
> if val3 has a single entry, but will do strange things if val3 is a
> vector:
>
> > val3 <- c("Monthly", "Daily")
> > ifelse(val3 == "Monthly", val<- 1, val<- 2)
> [1] 1 2
> > val
> [1] 2
>
>
> The ifelse() function does a vectorized test, and picks results from
> the two vector alternatives. Vincy wants a simple logical if, which
> can be computed in a few different ways:
>
> val <- if(val3 == "Monthly") val1 else val2
>
> or
>
> if (val3 == "Monthly") val <- val1
> else val <- val2
>
> For a simple calculation like this I'd probably use the former; if the
> calculation got more complex I'd prefer the latter.
>
> Duncan Murdoch
>
>>
>> Single "=" is for setting arguments within a function call. If you want
>> to test equality, then double "==" is required.
>> See ?"=="
>>
>> HTH,
>> Ivan
>>
>> Le 3/10/2011 11:45, Vincy Pyne a écrit :
>>> Dear R helpers
>>>
>>> Suppose
>>>
>>> val1 = c(10, 20, 35, 80, 12)
>>> val2 = c(3, 8, 11, 7)
>>>
>>> I want to select either val1 or val2 depending on value of third
>>> quantity val3.
>>>
>>> val3 assumes either of the values "Monthly" or "Yearly".
>>>
>>> If val3 = "Monthly", then val = val1 and if val3 = "Yearly", then
>>> val = val2.
>>>
>>> I tried the ifelse statement as
>>>
>>>
>>> ifelse(val3 = "Monthly", val = val1, val2)
>>>
>>> I get following error
>>>
>>>> ifelse(val3 = "Monthly", val = val1, val2)
>>> Error in ifelse(val3 = "Monthly", val = val1, val2) :
>>> unused argument(s) (val3 = "Monthly", val = val1)
>>>
>>>> val
>>> Error: object 'val' not found
>>>
>>> Kindly guide.
>>>
>>> Regards
>>>
>>> Vincy
>>>
>>>
>>>
>>> [[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.
>>
>
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra_at_uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.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 Thu 10 Mar 2011 - 12:36:14 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 Thu 10 Mar 2011 - 12:50:20 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