The Ultimate Guide to Relational Operators in R | by Linda Ngo | Towards Data Science

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 numbers3 == 2
Both of these output FALSE .
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 equals FALSE .
Check if -6 * 14 is equal to 17 — 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.
# Comparison of logicalsTRUE == FALSE# Comparison of numerics(-6 * 14) == (17 - 101)# Comparison of character strings"useR" == "user"# Comparison of a logical with a numericTRUE == 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 FALSETRUE != TRUE# Output TRUETRUE != FALSE# Output TRUE"hello" != "goodbye"# Output TRUE3 != 2
The result of the equality operator is the opposite for the inequality operator.
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 equals FALSE .
Check if — 6 * 14 is not equal to 17 — 101 .
Check if the strings “useR” and “user” are different.
Check if TRUE and 1 are equal.
# Comparison of logicalsTRUE == FALSE# Comparison of numerics(-6 * 14) != (17-101)# Comparison of character strings"useR" != "user"# Compare a logical with a numericTRUE == 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.
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 than FALSE.
# Comparison of numerics(-6 * 5 + 2) >= (-10 + 1)# Comparison of character strings"raining" <= "raining dogs"# Comparison of logicalsTRUE > 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.
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 vectorslinkedin <- c(16, 9, 13, 5, 2, 17, 14)facebook <- c(17, 7, 5, 16, 8, 13, 14)
# The linkedin and facebook vectorslinkedin <- c(16, 9, 13, 5, 2, 17, 14)facebook <- c(17, 7, 5, 16, 8, 13, 14)# Popular dayslinkedin > 15# Quiet dayslinkedin <= 5# LinkedIn more popular than Facebooklinkedin > 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 matrixlinkedin <- 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.
# The social datalinkedin <- 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.