[R] conditional IF with AND

rcoder mpdotbook at gmail.com
Wed Aug 13 21:57:55 CEST 2008


Thank you all for your replies. This is all very useful information for me!

Ted, thank you very much for the extra explanation and example.

Many thanks,

rcoder



Ted.Harding-2 wrote:
> 
> On 13-Aug-08 16:45:27, rcoder wrote:
>> Hi everyone,
>> I'm trying to create an "if" conditional statement with two conditions,
>> whereby the statement is true when condition 1 AND condition 2 are met:
>> 
>> code structure:
>> if ?AND? (a[x,y] <condition1>, a[x,y] <condition2>)
>> 
>> I've trawled through the help files, but I cannot find an example of
>> the syntax for incorporating an AND in a conditional IF statement.
>> 
>> Thanks,
>> rcoder
> 
> The basic structure of an 'if' statement (from ?"if" -- don't
> forget the ".." for certain keywords such as "if") is:
> 
>   if(cond) expr
> 
> What is not explained in the ?"if" help is that 'cond' may
> be any expression that evaluates to a logical TRUE or FALSE.
> 
> Hence you can build 'cond' to suit your purpose. Therefore:
> 
>   if( (<condition 1 on a[x,y]>)&(<condition 2 on a[x,y]>) ) {
>     <whatever you want to do if (cond1 AND cond2 ) is TRUE>
>   }
> 
> Example:
> 
>   if( (a[x,y]>1.0)&(a[x,y]<2.0) ){
>     print("Between 1 and 2")
>   }
> 
> Hoping this helps,
> Ted.
> 
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
> Fax-to-email: +44 (0)870 094 0861
> Date: 13-Aug-08                                       Time: 19:33:53
> ------------------------------ XFMail ------------------------------
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/conditional-IF-with-AND-tp18966890p18970101.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list