- 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 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
Relational Operators in R
Relational Operators are used to compare values in R objects. There are six relational operators:
Operator | Description |
---|---|
< | 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
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $29 (Regular $57)Free Guides - Getting Started with R and Python
Enter your name and email address below and we will email you the guides for R programming and Python.