R Fundamentals

Logical statements

In R, an expression or a statement consists of operators acting on variables of suitable data types.. For example,

c = a + b

Here, '+' is an addition operator that operates on integers a and b (called "operands"). Values of 'a' and 'b' are added and assigned to 'c'.

R has many operators for creating relational and logical expressions. They are mostly used in the control flow statements for executing simple or compound statements based on whether a given expression is evaluated to be true or false.

The syntax of logical expressions and control flow statements in R are very much similar to the corresponding constructs in C language.

The important equality, relational and the logical operators are listed below:

Operator Function Usage
< less than expression1 < expression2
<= less than or equal to expression1 <= expression2
> greater than expression1 > expression2
>= greater than or equal to expression1 >= expression2
== equality expression1 == expression2
!= inequality expression1 != expression2
& logical AND expression1 & expression2
|| logical OR expression1 || expression2
! logical NOT !expression1
isTrue() test if a variable is true isTRUE(x)
is.na() return TRUE if missing value is.na(x)

In the above table, "expression", "expression1" and "expression2" means expressions like,

        3.1456              (simple constant) 

        radius              (simple variable)  

        xvalue * sin(x)     (a compund expression)  

These operators can operate on one or more expressions.

To understand the way a logical expression works in R, have a look at the following tiny R-script and the output it generates:

x = 5 y = 6 print(x < y)

When these lines are executed as a script (say) "test1.r", the following output is generated:

> source("test1.r")
[1] TRUE

What has happened here? In the statement "print(x < y)", the result of "x < y" is evaluated. Since x is indeed less than y in the above script, the result evaluates to be TRUE. That is what is being printed by the "print(x < y)" statement. When a logical statement inside a pair of paranthesis is evaluated, the result is either "TRUE" or "FALSE".

The logical statements can be very simple (as above) or can be a compund statement. Some example statements are given below:

The statement " (x > y) & (x < z) " means " x greater than y and x less than z". This evaluates to TRUE when x=3,y=1 and z=4.

Similarly, the statement " ((x == 5) & (y == 6)) || (z > 10) means "either x equals 5 and y equals 6 should be true, or z should be greater than 10". This statement demands either a simultaneous conditions on valus of x and y to be true, or, alternately, z should have a value greater than 10. Note the clubbing of statements using pairs of brackets.

For a given set of values for the variables x, y and z, the evaluation of above logical statement proceeds in the following 4 steps:

     (i)   "(x == 5)" is evaluated (TRUE or FALSE) 

     (ii)  "(y == 6)" is evaluted (TRUE or FALSE) 

     (iii) "(z $>$ 10)" is evaluated (TRUE or FALSE)

     (iv)  Evaluate whether statements (i) and (ii) are true simultaneously.

     (v)   Evaluate whether at least one of the result of evaluations (iv) or (iii) are TRUE. 

Thus, for a set of values x=5, y=7 and z=12, the whole statement above evaluates to TRUE. For another set of values x=5, y=2 and z=9, above statement evalutes to be FALSE.