4.4 Comparisons

In R, comparisons will always return Logical data, i.e. either TRUE or False. We can use the following syntax to compare values:

< and >: greater than, less than
<= and >=: greater than or equal to, less than or equal to

For example:

print(5.3 < 17)
## [1] TRUE
print(4 >= (8/2))
## [1] TRUE

== checks if two values are equal. != checks if two values are different.

print(2 == (10 - 8))
## [1] TRUE

Note that a single equals sign = can be used to assign values. However, a double equals sign == is used to compare values.

4.4.1 AND and OR

We can evaluate multiple conditions using the logical AND and OR operators.

4.4.1.1 AND

AND statements are represented using the operator &. Two AND statements evaluate as TRUE only if both are TRUE. For example:

(5 > 2) & (7 + 2 == 9)
## [1] TRUE

If either statement is FALSE, the entire expression is FALSE:

(5 > 2) & (12 == 2)
## [1] FALSE

4.4.1.2 OR

OR statements are represented using the operator |. Two OR statements evaluate as TRUE if either statement is TRUE. For example:

(5 > 2) | (7 + 2 == 9)
## [1] TRUE

If either statement is TRUE, the entire expression is TRUE:

(5 > 2) | (12 == 2)
## [1] TRUE