Finance Train LogoFinance Train
Learning LibraryTemplatesBlog
Data Science Bundle
Finance TrainFinance Train
Learning LibraryTemplatesBlog
Data Science Bundle

Relational Operators in R

This is a sample chapter from the ebook R Programming for Data Science.

Relational Operators are used to compare values in R objects. There are six relational operators:

1| Operator | Description |
2| --- | --- |
3| `<` | Less than |
4| `>` | Greater than |
5| `<=` | Less than or equal to |
6| `>=` | Greater than or equal to |
7| `==` | Equal to |
8| `!=` | Not equal to |
9

Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.

Let's look at each of these operators in detail.

Greater Than (>)

The greater than (>) symbol checks if each element of the first vector is greater than the corresponding element of the second vector. The result will be a vector with logical values (TRUE or FALSE) depending on whether the condition is true or not.

1The following are two vectors containing returns from two stocks over the past five days.
2stock_A <- c(10, 8, 9, 11, 12)
3stock_B <- c(8, 11, 10, 10, 12)
4# Compare if Stock A's returns are higher than Stock B's returns
5stock_A > stock_B
6

The result will be as follows:

[1] TRUE FALSE FALSE TRUE FALSE

As you can see, Stock A's returns were higher than Stock B on day 1 (10 > 8) and day 4 (11 > 10). So, for these days, the resulting vector contains TRUE, while for the rest of the days, it is FALSE. Specially observe the last day where the returns were same for both the stocks (12 and 12). Since the condition was exclusively checking for returns being greater, the condition is FALSE here.

Less Than (<)

The less than (<) symbol checks if each element of the first vector is less than the corresponding element of the second vector. This is just the opposite of the 'greater than' operator.

1> stock_A <- c(10, 8, 9, 11, 12)
2> stock_B <- c(8, 11, 10, 10, 12)
3> 
4> #compare if Stock A's returns are less than Stock B's returns
5> 
6> stock_A < stock_B
7[1] FALSE  TRUE  TRUE FALSE FALSE
8

Equal (==)

The equal (==) operator checks if each element of the first vector is equal to the corresponding element of the second vector. In our vectors, this condition meets only for the returns on the last day (12 == 12) as shown below:

1> stock_A <- c(10, 8, 9, 11, 12)
2> stock_B <- c(8, 11, 10, 10, 12)
3> 
4> #compare if Stock A's returns are equal to Stock B's returns
5> 
6> stock_A == stock_B
7[1] FALSE FALSE FALSE FALSE  TRUE
8

Not Equal To (!=)

The Not Equal (!=) operator checks if each element of the first vector is unequal to the corresponding element of the second vector.

1> # The following are two vectors containing returns from two stocks over the past five days.
2> 
3> stock_A <- c(10, 8, 9, 11, 12)
4> stock_B <- c(8,11,10,10,12)
5> 
6> #compare if Stock A's returns are not equal to Stock B's returns
7> 
8> stock_A != stock_B
9[1]  TRUE  TRUE  TRUE  TRUE FALSE
10

Greater Than Equal To (>=)

The Greater Than Equal To (>=) operator checks if each element of the first vector is greater than or equal to the corresponding element of the second vector.

1> # The following are two vectors containing returns from two stocks over the past five days.
2> 
3> stock_A <- c(10, 8, 9, 11, 12)
4> stock_B <- c(8,11,10,10,12)
5> 
6> #compare if Stock A's returns are greater than or equal to Stock B's returns
7> 
8> stock_A >= stock_B
9[1]  TRUE FALSE FALSE  TRUE  TRUE
10

Compare this with the Greater Than (>) operator. Everything else is same, just the last element now returns TRUE (because of the equal to condition).

Less Than Equal To (<=)

The Less Than Equal To (<=) operator checks if each element of the first vector is less than or equal to the corresponding element of the second vector.

1> stock_A <- c(10, 8, 9, 11, 12)
2> stock_B <- c(8,11,10,10,12)
3> 
4> #compare if Stock A's returns are lower than or equal to Stock B's returns
5> 
6> stock_A <= stock_B
7[1] FALSE  TRUE  TRUE FALSE  TRUE
8

Ebook Sample

You're reading a sample chapter from R Programming for Data Science

Get the ebook to continue with the full material and included resources.

This Ebook

R Programming for Data Science

$7.00

Learning Library

Practical resources on data science, machine learning, and AI for finance professionals.

Browse Library
Mini Roadmap - Data Science for Finance
Free Download

Mini Roadmap: Data Science for Finance

A step-by-step guide covering Python, SQL, analytics, and finance applications.

Finance Train

Learn data science and AI skills for finance through practical courses and tutorials.

Learn

  • Learning Library
  • Course Directory
  • Blog

Resources

  • Templates & Downloads
  • Tools
  • Tables
  • Calculators

Company

  • About
  • Contact
  • Privacy
  • Terms

© 2026 Finance Train. All rights reserved.