Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In R, boolean variables can take only 2 values – TRUE and FALSE.

For example,

# declare boolean
x <- TRUE
print(x)
print(class(x))

# declare boolean using single character
y <- F
print(y)
print(class(y))

Output

 TRUE
 "logical"
 FALSE
 "logical"

Here, we have declared x and y as boolean variables. In R, Boolean variables belong to the logical class.

You can also declare boolean variables using a single character - T or F. Here, T stands for TRUE and F stands for FALSE.

## R Boolean With Comparison Operators

Comparison operators are used to compare two values.

Operator Description Example
> Greater than 5 > 6 returns FALSE
< Less than 5 < 6 returns TRUE
== Equals to 10 == 10 returns TRUE
!= Not equal to 10 != 10 returns FALSE
>= Greater than or equal to 5 >= 6 returns FALSE
<= Less than or equal to 6 <= 6 returns TRUE

The output of a comparison is a boolean value. For example, to check if two numbers are equal, you can use the == operator.

x <- 10
y <- 23

# compare x and y
print(x == y)  # FALSE

Similarly, to check if x is less than y, you can use the < operator.

x <- 10
y <- 23

# compare x and y
print(x < y)  # TRUE

Since, the value stored in x is less than the value stored in y, the comparison x < y results in TRUE.

## R Boolean With Logical Operators

Logical operators are used to compare the output of two comparisons. There are three types of logical operators in R. They are:

• AND operator (&)
• OR operator (|)
• NOT operator (!)

### AND Operator (&)

The AND operator & takes as input two logical values and returns the output as another logical value.

The output of the operator is TRUE only when both the input logical values are either TRUE or evaluated to TRUE.

Let a and b represent two operands. 0 represents FALSE and 1 represents TRUE. Then,

a b a & b
1 1 1
1 0 0
0 1 0
0 0 0

For example,

# print & of TRUE and FALSE combinations
TRUE & TRUE
TRUE & FALSE
FALSE & TRUE
FALSE & FALSE

Output

 TRUE
 FALSE
 FALSE
 FALSE

The input to any logical operator can also be a comparison between two or more variables. For example,

x <- 10
y <- 23
z <- 12

print(x<y & y>z)

Output

 TRUE

Here, the condition checks whether x is less than y and y is less than z or not. If both the conditions evaluate to TRUE, then the output is TRUE. If any of them is FALSE, the output turns out to be FALSE.

### OR Operator (|)

The OR operator | returns TRUE if all or any one of the logical inputs is TRUE or evaluates to TRUE. If all of them are FALSE, then it returns FALSE. Consider the table below.

a b a | b
1 1 1
1 0 1
0 1 1
0 0 0

For example,

# print | of TRUE and FALSE combinations
TRUE | TRUE
TRUE | FALSE
FALSE | TRUE
FALSE | FALSE

Output

 TRUE
 TRUE
 TRUE
 FALSE

Here, if any one of the inputs is TRUE, then the output is TRUE.

Similar to the case of AND operator, you can use any number of comparisons as input to the OR operator. For example,

w <- 54
x <- 12
y <- 25
z <- 1

print(w>x | x>y | z>w)

Output

 TRUE

Here, only the comparison w>x evaluates to TRUE. Apart from that, all the other comparisons evaluate to FALSE. Since, at least one of the inputs is TRUE, the output of the entire comparison is TRUE.

### NOT (!) Operator

The NOT operator ! is used to negate the logical values it is used on. If the input value is TRUE, it will turn to FALSE and vice-versa.

a !a
1 0
0 1

For example,

# print ! of TRUE and FALSE
!TRUE
!FALSE

Output

 FALSE
 TRUE

Here, the output is the negation of the input.

We can use the ! operator with comparisons. For example, !(x > 12) is the same as x <= 12. This means that x is not greater than 12. Which means that x can be less than or equal to 12.

You can also use the NOT operator with any in-built function that evaluates to boolean value. For example,

x <- 3 + 5i

# using ! with in-built function
print(!is.numeric(x))

Output

 TRUE

Here, since x is of type complex, the function is.numeric(x) evaluates to FALSE and the negation of FALSE is TRUE, hence the output.

## Example: R Comparison and Logical Operators

You can use all the three logical operators with comparison operators.

x <- 5

print(is.numeric(x) & (x>5 | x==5))

Output

 TRUE

Here, we can consider the entire operation in two parts - is.numeric(x) and (x>5 | x==5). Since, there is an AND operator between them, if both of them evaluate to TRUE, only then the output is TRUE.

This is how the program works:

• is.numeric(x) - this evaluates to TRUE since x is of numeric type
• (x>5 | x==5) - this evaluates to TRUE since x==5 is TRUE