Logical Operators

In this lesson, we will learn the basic logical operations in R and how to use them.

We'll cover the following

What are Logical Operators?

Logical operators are used for carrying out Boolean operations like AND, OR etc. Such operators compile the results of multiple logical tests into a single TRUE or FALSE.

Following are the logical operators in R language:

widget

Let’s dive right into the coding part:

Press + to interact
number1 <- 10
number2 <- 3
number1 & number2 # element wise logical AND between number1 and number2
number1 && number2 # logical AND between number1 and number2
number1 | number2 # element wise logical OR between number1 and number2
number1 || number2 # logical OR between number1 and number2
!number1 # NOT of number1
xor(number1, number2) # XOR of number1 and number2

All these statements either return TRUE or FALSE.