# Logical Operators in R

In programming, you will often come across situations where you will need to evaluate multiple conditions. For example, you may want to perform an action when the price of a stock is above 10 but below 12. Logical operators come into play in such situations. These logical operators allow a program to make a decision based on multiple conditions.

In R, there are five logical operators.

### AND (&)

This is called the Logical AND Operator. The logical AND operator (&) returns the boolean value true if both operands are true and returns false otherwise. The following examples illustrate this:

#Assign values to Stock A and Stock B
stock_A = 10
stock_B = 12
#Test if Stock A is equal to 10 and Stock B is equal to 12.
(stock_A==10) & (stock_B==12)


This will return TRUE if both the conditions evaluate to TRUE. The result will be TRUE as shown below:

> (stock_A==10) & (stock_B==12)
 TRUE


Below is another example of & operator on two vectors.

# The following are two vectors containing returns from two stocks over the past five days.
stock_A <- c(10, 8, 9, 11, 12)
stock_B <- c(8,11,10,10,12)
# Stock A exceeds 10 (Inclusive) and Stock B is below 10 (Inclusive)
(stock_A >=10) & (stock_B <=10)


The conditions are satisfied at element level. For the first element, both conditions are true (Stock A: 10>=10 and Stock B: 8 <=10). Since we are using AND operator, both conditions should be TRUE, which it is in our case, so, for the first element, the result will be TRUE. For the 2nd element both conditions are FALSE. Stock A value is 8 which is not greater than or equal to 10. Similarly for Stock B actual value is 11, which is not less than or equal to 10 as required by our condition. Since both conditions are FALSE, the result will be FALSE. The only time the result will be TRUE will be when both the conditions are TRUE.

The results are shown below:

> (stock_A >=10) & (stock_B <=10)
  TRUE FALSE FALSE  TRUE FALSE


### AND (&&)

If you have used other programming languages then you would know that && is also used for AND operator. It is indeed used, but there is a big difference. The AND operator with two ampersand signs (&&) is called longer form operator and in R, the longer form evaluates left to right examining only the first element of each vector.

So if you use && on single values, there is no difference, but if you use it on vectors, only the first elements of the vectors would be evaluated.

> #Assign values to Stock A and Stock B
> stock_A = 10
> stock_B = 12
>
> #Test if Stock A is equal to 10 and Stock B is equal to 12.
> (stock_A==10) && (stock_B==12)
 TRUE
>
> # The following are two vectors containing returns from two stocks over the past five days.
>
> stock_A <- c(10, 8, 9, 11, 12)
> stock_B <- c(8,11,10,10,12)
>
> # Stock A exceeds 10 (Inclusive) and Stock B is below 10 (Inclusive)
>
> (stock_A >=10) && (stock_B <=10)
 TRUE
>


As you can see, only the first element of the vector is evaluated.

### OR (|)

This is called the Logical OR Operator. The logical OR operator (|) returns the boolean value true if either operand is true and returns false otherwise.

> #Assign values to Stock A and Stock B
> stock_A = 10
> stock_B = 12
>
> #Test if Stock A is equal to 10 or Stock B is equal to 12.
> (stock_A==10) | (stock_B==15)
 TRUE
>
> # The following are two vectors containing returns from two stocks over the past five days.
>
> stock_A <- c(10, 8, 9, 11, 12)
> stock_B <- c(8,11,10,10,12)
>
> # Stock A exceeds 10 (Inclusive) OR Stock B is below 10 (Inclusive)
>
> (stock_A >=10) | (stock_B <=10)
  TRUE FALSE  TRUE  TRUE  TRUE


### OR (||)

The long form OR operator (||) works just like the long form AND operator. Only the first element of the vector is evaluated.

### NOT (!)

This is called Logical NOT operator. It takes each element of the vector and gives the opposite logical value.

> #Assign values to Stock A and Stock B
> stock_A = 10
> stock_B = 12
>
> #Examples of Logical NOT Operator
> !((stock_A==10) | (stock_B==15))
 FALSE
> !TRUE
 FALSE
> !(5>10)
 TRUE


Now that we know about relational and logical operators, we can move to the next topic - Conditional Statements.  