[R] conditional IF with AND

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Aug 13 20:33:56 CEST 2008


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



More information about the R-help mailing list