The Ultimate Guide to Relational Operators in R (2024)

Relational operators help us see how objects relate to one another. Learn Everything you need to know to conquer them!

The Ultimate Guide to Relational Operators in R (1)

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 .

The Ultimate Guide to Relational Operators in R (2)

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"
The Ultimate Guide to Relational Operators in R (3)

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.

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 Ultimate Guide to Relational Operators in R (4)

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.

The Ultimate Guide to Relational Operators in R (5)

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 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.

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
The Ultimate Guide to Relational Operators in R (6)

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.

The Ultimate Guide to Relational Operators in R (7)

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.

The Ultimate Guide to Relational Operators in R (8)

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 than FALSE.

Solution

# Comparison of numerics
(-6 * 5 + 2) >= (-10 + 1)
# Comparison of character strings
"raining" <= "raining dogs"
# Comparison of logicals
TRUE > FALSE
The Ultimate Guide to Relational Operators in R (9)

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
The Ultimate Guide to Relational Operators in R (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.

The Ultimate Guide to Relational Operators in R (11)

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
The Ultimate Guide to Relational Operators in R (12)

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
The Ultimate Guide to Relational Operators in R (13)

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.

The Ultimate Guide to Relational Operators in R (2024)

FAQs

How to use relational operators in R? ›

How to use relational operators in R?

What does %% mean in R programming? ›

What does %% mean in R programming?

What is the use of <- operator in R? ›

What is the use of <- operator in R?

How many operators are there in R programming? ›

How many operators are there in R programming?

Top Articles
The Five Happiest Interior Colors to Use in Your Home - According to LiLu Interiors - www.liluinteriors.com
How to Make a Salami Rose (2 ways!) - The Recipe Critic
Asist Liberty
Fat Hog Prices Today
Mountain Dew Bennington Pontoon
Belle Meade Barbershop | Uncle Classic Barbershop | Nashville Barbers
Kraziithegreat
Robinhood Turbotax Discount 2023
27 Places With The Absolute Best Pizza In NYC
How to Watch Braves vs. Dodgers: TV Channel & Live Stream - September 15
Which aspects are important in sales |#1 Prospection
shopping.drugsourceinc.com/imperial | Imperial Health TX AZ
Craigslist Labor Gigs Albuquerque
Cranberry sauce, canned, sweetened, 1 slice (1/2" thick, approx 8 slices per can) - Health Encyclopedia
What Happened To Maxwell Laughlin
Dr Manish Patel Mooresville Nc
Letter F Logos - 178+ Best Letter F Logo Ideas. Free Letter F Logo Maker. | 99designs
Char-Em Isd
Invert Clipping Mask Illustrator
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Where to Find Scavs in Customs in Escape from Tarkov
Officialmilarosee
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
What Channel Is Court Tv On Verizon Fios
Canvasdiscount Black Friday Deals
Craigslist Maryland Trucks - By Owner
Ihub Fnma Message Board
3Movierulz
D2L Brightspace Clc
Best Middle Schools In Queens Ny
January 8 Jesus Calling
Infinite Campus Asd20
Town South Swim Club
Winterset Rants And Raves
Craigslist Scottsdale Arizona Cars
Kempsville Recreation Center Pool Schedule
Acuity Eye Group - La Quinta Photos
Kokomo Mugshots Busted
Memberweb Bw
Vip Lounge Odu
Etowah County Sheriff Dept
Cdcs Rochester
What Is Kik and Why Do Teenagers Love It?
303-615-0055
The Listings Project New York
FREE - Divitarot.com - Tarot Denis Lapierre - Free divinatory tarot - Your divinatory tarot - Your future according to the cards! - Official website of Denis Lapierre - LIVE TAROT - Online Free Tarot cards reading - TAROT - Your free online latin tarot re
Lyndie Irons And Pat Tenore
Fatal Accident In Nashville Tn Today
Candise Yang Acupuncture
Swsnj Warehousing Inc
Streameast Io Soccer
Craiglist.nj
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6360

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.