# Vector example
<- c(1, 2, 3, 4, 5)
x > 3 x
[1] FALSE FALSE FALSE TRUE TRUE
Logical tests are used extensively in coding in general, and coding in R is no exception. By a logical test, we mean an expression that evaluates to either TRUE
or FALSE
(or sometimes NA
).
Certain logical tests (but not all) involve the use of comparison operators. The most common comparison operators are:
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | x == 5 |
TRUE if x is 5, otherwise FALSE |
!= |
Not equal to | x != 5 |
TRUE if x is not 5, otherwise FALSE |
< |
Less than | x < 5 |
TRUE if x is less than 5, otherwise FALSE |
> |
Greater than | x > 5 |
TRUE if x is greater than 5, otherwise FALSE |
<= |
Less than or equal to | x <= 5 |
TRUE if x is less than or equal to 5, otherwise FALSE |
>= |
Greater than or equal to | x >= 5 |
TRUE if x is greater than or equal to 5, otherwise FALSE |
Let’s pick apart a few of these for a moment:
==
– Why can’t we just use =
? Although we’ve been using <-
for assignment in R, but =
can also be used for assignment. So, we need a different operator, ==
, when we want to compare whether two values are equal.
!=
– This operator checks if two values are not equal. It is the opposite of ==
. Notice that !
is used to indicate negation, and we can also use it to turn TRUE
into FALSE
and vice versa.
<
, >
, <=
, >=
– These operators are used not only to compare numerical values, but they can also be used to compare character data (for example, 'aa' < 'ab'
is TRUE
) and date values (for example, as.Date('2025-01-01') > as.Date('2023-07-06')
is TRUE
)
When we use comparison operators on vectors or matrices, R will perform the comparison element-wise. For example:
# Vector example
<- c(1, 2, 3, 4, 5)
x > 3 x
[1] FALSE FALSE FALSE TRUE TRUE
# Matrix example - first create the matrix
<- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
x x
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
# Matrix example - perform the test
>= 4 x
[,1] [,2] [,3]
[1,] FALSE FALSE TRUE
[2,] FALSE TRUE TRUE
There’s also a special operator, %in%
, that we can use to return a TRUE
or FALSE
depending on whether a particular value is or is not present in a vector or matrix:
<- c('Ali', 'Bob', 'Charlotta')
names 'Divya' %in% names
[1] FALSE
Logical tests are used in conditional statements, such as if
statements, and in loops, such as for
and while
loops. They are also used in subsetting data frames and vectors.