[R] How to use conditional statement

peter dalgaard pdalgd at gmail.com
Thu Mar 10 16:37:15 CET 2011


On Mar 10, 2011, at 12:54 , Duncan Murdoch wrote:

> 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
> 

Notice also that val will end up as 2, even if you reverse the order of val3. As long as the condition is not all false or all true, both arguments to ifelse are evaluated, and the last assignment will "win". You _really_ do not want to do assignment in function arguments in R. Or, if you do, you'd better know _exactly_ what you are doing.


> 
> The ifelse() function does a vectorized test, and picks results from the two vector alternatives.  Vincy wants a simple logical

Yes, although that is only clear from the different lengths of val1 and val2. Otherwise

val <- ifelse(val3 == "Monthly", val1, val2)

would be the other option (with val1, val2, val3 of same length).

> 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.
>> 
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list