• 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

Logical Operators in R

Data Science

R Programming for Data Science Logical Operators in R

In programming, you will often come across situations where you will need to evaluate multiple conditions. For example, you may want to perform an action when the price of a stock is above 10 but below 12. Logical operators come into play in such situations. These logical operators allow a program to make a decision based on multiple conditions.

In R, there are five logical operators.

AND (&)

This is called the Logical AND Operator. The logical AND operator (&) returns the boolean value true if both operands are true and returns false otherwise. The following examples illustrate this:

#Assign values to Stock A and Stock B
stock_A = 10
stock_B = 12
#Test if Stock A is equal to 10 and Stock B is equal to 12.
(stock_A==10) & (stock_B==12)

This will return TRUE if both the conditions evaluate to TRUE. The result will be TRUE as shown below:

> (stock_A==10) & (stock_B==12)
[1] TRUE

Below is another example of & operator on two vectors.

# 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)
# Stock A exceeds 10 (Inclusive) and Stock B is below 10 (Inclusive)
(stock_A >=10) & (stock_B <=10)

The conditions are satisfied at element level. For the first element, both conditions are true (Stock A: 10>=10 and Stock B: 8 <=10). Since we are using AND operator, both conditions should be TRUE, which it is in our case, so, for the first element, the result will be TRUE. For the 2nd element both conditions are FALSE. Stock A value is 8 which is not greater than or equal to 10. Similarly for Stock B actual value is 11, which is not less than or equal to 10 as required by our condition. Since both conditions are FALSE, the result will be FALSE. The only time the result will be TRUE will be when both the conditions are TRUE.

The results are shown below:

> (stock_A >=10) & (stock_B <=10)
[1]  TRUE FALSE FALSE  TRUE FALSE

AND (&&)

If you have used other programming languages then you would know that && is also used for AND operator. It is indeed used, but there is a big difference. The AND operator with two ampersand signs (&&) is called longer form operator and in R, the longer form evaluates left to right examining only the first element of each vector.

So if you use && on single values, there is no difference, but if you use it on vectors, only the first elements of the vectors would be evaluated.

> #Assign values to Stock A and Stock B
> stock_A = 10
> stock_B = 12
> 
> #Test if Stock A is equal to 10 and Stock B is equal to 12.
> (stock_A==10) && (stock_B==12) 
[1] TRUE
> 
> # 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)
> 
> # Stock A exceeds 10 (Inclusive) and Stock B is below 10 (Inclusive)
> 
> (stock_A >=10) && (stock_B <=10)
[1] TRUE
>

As you can see, only the first element of the vector is evaluated.

OR (|)

This is called the Logical OR Operator. The logical OR operator (|) returns the boolean value true if either operand is true and returns false otherwise.

> #Assign values to Stock A and Stock B
> stock_A = 10
> stock_B = 12
> 
> #Test if Stock A is equal to 10 or Stock B is equal to 12.
> (stock_A==10) | (stock_B==15) 
[1] TRUE
> 
> # 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)
> 
> # Stock A exceeds 10 (Inclusive) OR Stock B is below 10 (Inclusive)
> 
> (stock_A >=10) | (stock_B <=10)
[1]  TRUE FALSE  TRUE  TRUE  TRUE

OR (||)

The long form OR operator (||) works just like the long form AND operator. Only the first element of the vector is evaluated.

NOT (!)

This is called Logical NOT operator. It takes each element of the vector and gives the opposite logical value.

> #Assign values to Stock A and Stock B
> stock_A = 10
> stock_B = 12
> 
> #Examples of Logical NOT Operator
> !((stock_A==10) | (stock_B==15))
[1] FALSE
> !TRUE
[1] FALSE
> !(5>10)
[1] TRUE

Now that we know about relational and logical operators, we can move to the next topic – Conditional Statements.

Previous Lesson
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