## Using relational operators

___ == ___
___ != ___
___ < ___
___ > ___
___ <= ___
___ >= ___
View Interactive Version

Relational operators are used to compare two values. The output of these operations is always a logical value TRUE or FALSE. We distinguish six different types relational operators, as we’ll see below.

The equal == and not equal != operators check whether two values are the same (or not):

Input
2 == 1 + 1
Output
[1] TRUE
Input
2 != 3
Output
[1] TRUE

The less than < and greater than > operators check, whether a value is less or greater than another one:

Input
2 > 4
Output
[1] FALSE
Input
2 < 4
Output
[1] TRUE

The less than or equal to <= and the greater than or equal to >= operators combine the check for equality with either the less or the greater than comparison:

Input
2 >= 2
Output
[1] TRUE
Input
2 <= 3
Output
[1] TRUE

All of these operators can be used on vectors with one or more elements as well. In that case, each element of one vector is compared with the element at the same position in the other vector, just as with the mathematical operators:

Input
vector1 <- c(3, 5, 2, 7, 4, 2)
vector2 <- c(2, 6, 3, 3, 4, 1)
vector1 > vector2
Output
[1]  TRUE FALSE FALSE  TRUE FALSE  TRUE

Therefore, the output of this example is based on the comparisons 3 > 2, 5 > 6, 2 > 3 and so on.

Use basic operators