Relational operators help us see how objects relate to one another. Learn Everything you need to know to conquer them!
Relational operators, or comparators, are operators which help us see how one R object relates to another.
For example, you can check whether two objects are equal (equality) by using a double equals sign ==
.
We can see if the logical value of TRUE
equals the logical value of TRUE
by using this query TRUE == TRUE
. The result of the equality query is a logical value ( TRUE or FALSE
). In this case, it is TRUE
because TRUE
equals TRUE
.
On the contrary, TRUE == FALSE
will give us FALSE
.
Apart from logical variables, we can also check the equality of other types, such as strings and numbers.
# Comparing the equality of two strings
"hello" == "goodbye"# Comparing the equality of two numbers
3 == 2
Both of these output FALSE
.
For you to try
The most basic form of comparison is equality. Recall that it is represented by the double equations syntax, ==
. Here is an example of some equality statements:
3 == (2 + 1)
"ultimate guide" == "r"
TRUE == FALSE
"Rchitect" == "rchitect"
Notice from the last expression that R is case sensitive: “R” is not equal to “r”.
Try out the following comparisons:
- Write R code to see if
TRUE
equalsFALSE
. - Check if
-6 * 14
is equal to17 — 101
. - See if the strings
"useR"
and"user"
are equal in R. - Find out what happens if you compare
TRUE
to the numeric 1.
Make sure not to mix up ==
(comparison) and =
(assignment), ==
is what is used to check equality of R objects.
Solution
# Comparison of logicals
TRUE == FALSE# Comparison of numerics
(-6 * 14) == (17 - 101)# Comparison of character strings
"useR" == "user"# Comparison of a logical with a numeric
TRUE == 1
The opposite of the equality operator is the inequality operators, written as an exclamation mark followed by an equals sign ( !=
).
For example, the sentence "hello" != "goodbye"
would read as: “hello” is not equal to “goodbye”. Because this statement is correct, R will output TRUE
.
The inequality operator can also be used for numerics, logicals, and other R objects.
# Output FALSE
TRUE != TRUE# Output TRUE
TRUE != FALSE# Output TRUE
"hello" != "goodbye"# Output TRUE
3 != 2
The result of the equality operator is the opposite for the inequality operator.
For you to try
The inequality comparator is simply the opposite of equality. The following statements all evaluate to TRUE
:
3 == (2 + 1)
"intermediate" != "r"
TRUE != FALSE
"Rchitect" != "rchitect"
Write out expressions that do the following:
- Check if
TRUE
equalsFALSE
. - Check if
— 6 * 14
is not equal to17 — 101
. - Check if the strings
“useR”
and“user”
are different. - Check if
TRUE
and 1 are equal.
Solution
# Comparison of logicals
TRUE == FALSE# Comparison of numerics
(-6 * 14) != (17-101)# Comparison of character strings
"useR" != "user"# Compare a logical with a numeric
TRUE == 1
There are also cases where we need more than simply equality and inequality operators. For instance, what about checking if an R object is ‘less than’ or ‘greater than’ another R object? In this case, we can use the less-than <
and greater-than >
sign for this.
In the case of numerical values, it is pretty straightforward. For example, 3 is less than 5, so 3 < 5
will evaluate to TRUE
, while 3 greater than 5 (3 > 5
) will evaluate to FALSE
.
For numerics, this makes sense. But how would this work for character strings and logical values?
For character strings, R uses the alphabet to sort them. So, "Hello" > "Goodbye"
would evaluate to TRUE
since “H” comes after “G” in the alphabet, and R consider it greater.
For logical values, TRUE
corresponds to 1 and FALSE
corresponds to 0. So is TRUE
less than FALSE
? No, because 1 is not less than 0, hence the FALSE
result.
We can also check to see if one R object is greater than or equal to (or less than or equal to) another R object. To do this, we can use the less than sign, or the greater than sign, together with the equals sign.
So, 5 greater than or equal to 3 5 >= 3
, as well as 3 greater than or equal to 3 3 >= 3
will evaluate as TRUE
.
For you to try
Apart from equality operators ( ==
and !=
), we also learned about the less than and greater than operators: <
and >
. We can also add an equal sign to express less than or equal to or greater than or equal to, respectively. For example, the following all evaluate to FALSE
:
(1+2) > 4
"dog" < "Cats"
TRUE <= FALSE
Remember that for string comparison, R determines the greater than relationship based on alphabetical order. Also keep in mind that TRUE
is treated as 1
for arithmetic, and FALSE
is treated as 0
. Therefore, FALSE < TRUE
is TRUE
.
Write R expressions to check whether:
-6 * 5 + 2
is greater than or equal to-10 + 1
.“raining”
is less than or equal to“raining dogs”
TRUE
is greater thanFALSE
.
Solution
# Comparison of numerics
(-6 * 5 + 2) >= (-10 + 1)# Comparison of character strings
"raining" <= "raining dogs"# Comparison of logicals
TRUE > FALSE
We already know that R is pretty good with vectors from the Introduction to Vectors post. Without having to change anything about the syntax, R’s relational operators also work on vectors.
Suppose you have recorded the daily number of views your LinkedIn profile had in the previous link and stored them in a vector, linkedin
.
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
If we want to find out on which days the number of views exceeded 10, we can directly use the greater than sign.
linkedin > 10
For the first, third, sixth, and seventh element in the vector, the number of views is greater than 10, so for these elements the result will be TRUE
.
We can also compare vectors to vectors. Suppose you also recorded the number of views your Facebook profile had the previous week and saved them in another vector facebook
.
facebook <- c(17, 7, 5, 16, 8, 13, 14)
When are the number of Facebook views less than or equal to the number of LinkedIn views? We can use the following expression to calculate this.
facebook <= linkedin
In this case, the comparison is done for every element of the vector, one by one. For example, in the third day, the number of Facebook views is 5 and the number of LinkedIn views is 13. The comparison evaluates to TRUE
, as 5 is smaller than or equal to 13. This means that the number of Facebook views is less than or equal to the number of LinkedIn views on the third day.
For you to try
Using the same social media vectors above, linkedin
and facebook
, which contain the number of profile views over the last seven days, use relational operators to find a logical answer ( TRUE
or FALSE
) for the following questions:
- On which days did the number of LinkedIn profile views exceed 15?
- When was your LinkedIn profile viewed only 5 times or fewer?
- When was your LinkedIn profile visited more often than your Facebook profile?
# The linkedin and facebook vectors
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
Solution
# The linkedin and facebook vectors
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)# Popular days
linkedin > 15# Quiet days
linkedin <= 5# LinkedIn more popular than Facebook
linkedin > facebook
From the output, we can determine the following:
- Your LinkedIn profile views exceed 15 on the first and sixth day.
- Your LinkedIn profile was only viewed 5 or less times on the fourth and fifth day.
- Your LinkedIn profile was visited more than your Facebook profile on the second, third, and sixth day.
Up to now, we’ve learned and compared logicals, numerics, strings, and vectors. However, R’s ability to deal with different data structures for comparisons does not stop at matrices. Matrices and relational operators also work together seamlessly!
Suppose, instead of in vectors (like in the previous for you to try), the LinkedIn and Facebook data is stored in a matrix called views
instead. The first row contains the LinkedIn information; the second row the Facebook information.
# The social data stored in a matrix
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)
Using the relational operators you’ve learned, try to determine the following:
- When were the views exactly equal to 13? Use the
views
matrix to return a logical matrix. - For which days were the number of views less than or equal to 14? Again, have R return a logical matrix.
Solution
# The social data
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)# When does views equal 13?
views == 13# When is views less than or equal to 14?
views <= 14
From the output we can determine:
- On day 3, there were 13 LinkedIn views. On day 6, there were 13 Facebook views.
- On days 2, 3, 4, 5, and 7, there were less than or equal to 14 LinkedIn views. On days 2, 3, 5, 6, and 7, there were less than or equal to 14 Facebook views.
All images, unless specified, are owned by the author. The banner image was created using Canva.