• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Relational Operators in R

Data Science

R Programming for Data Science Relational Operators in R

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

OperatorDescription
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
==Equal to
!=Not equal to

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.

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

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.

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

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:

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

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.

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

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.

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

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.

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

Back to Course
Next Lesson

Primary Sidebar

In this Course

Course Home
R - Core Programming Principles
Relational Operators in R
Logical Operators in R
Conditional Statements in R
For Loop in R Programming
While and Repeat Loop in R Programming
Functions in R Programming
Creating Functions in R
Apply Functions in R
Importing Data in R
Importing Data from External Data Sources in R
Importing Data Using read.csv in R
Import Data using read.table in R
Importing Data Using data.table – fread in R
Importing Data from Excel in R
Using XLConnect in R Programming
Importing Data from a Database in R
SQL Queries from R
Importing Data from Web in R
Return to R Programming for Data Science

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us