• 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

Conditional Statements in R

Data Science

R Programming for Data Science Conditional Statements in R

In programming, we often want to check a conditional statement and then do something if the condition is met and do something else if the condition is not met. This is done using the If conditions.

There are two general forms of decision making structures found in most of the programming languages:

if statement

if(BooleanExpression)
    statement;
if(BooleanExpression) {
    statement1;
    statement2;
    ...
}

If the BoolenExpression is true, then the statements will be executed.

if…else statement

    statement or block 1
else
    statement or block 2

Examples

Let’s look at a few examples of the conditional statements.

#Imaginary stock prices
stock_A <- 100
stock_B <- 120
#Check if Stock B's price is above 100.
if(stock_B>100) {
  print("We recommend Stock B")
}

When the above code is compiled and executed, it produces the following result:

[1] "We recommend Stock B"

Let’s take another example to evaluate an If…Else statement.

#Imaginary stock prices
stock_A <- 100
stock_B <- 120
#Check if Stock A's price is greater than Stock B's price.
if(stock_A>stock_B) {
  print("We recommend Stock A")
} else {
  print("We recommend Stock B")
}

The above code will produce the following results:

[1] "We recommend Stock B"

As you can see, the Boolean Express (stock_A>stock_B) is false. Therefore, the statements in the ‘else’ block are evaluated.

The if…else if…else Statement

We can use else if to further customize our conditional statement. With the if else we can extend the if statement as much as you want. In the entire control structure, as soon as a condition is met (TRUE) the corresponding block of statements will be executed, and the rest of the structure will be ignored.

if (BooleanExpression1) {
  expression1
} else if (BooleanExpression2) {
  expression2
} else if (BooleanExpression3) {
  expression3
} else {
  expression4
}

If BooleanExpression1 is TRUE, then expression1 will be executed. If FALSE, then it will check for BooleanExpression2. If BooleanExpression2 is TRUE, then expression2 will be execued, otherwise, it will evaluate BooleanExpression3. If BooleanExpression3 is TRUE, expression3 will be executed, otherwise, expression 4 will be executed.

Let’s look at an example:

#Imaginary stock prices
stock_A <- 100
stock_B <- 120
#Compare Stock A and Stock B
if (stock_A>stock_B) {
  print("We recommend Stock A")
} else if (stock_A==stock_B) {
  print("We are neutral to both stocks")
} else {
  print("We recommend Stock B")
}

The above code will produce the following results:

[1] "We recommend Stock B"

In our if conditions, we can use logical operators to check for multiple conditions, as shown below:

#Imaginary stock prices
stock_A <- 100
stock_B <- 120
if (stock_A>=100 & stock_B>=100) {
  print("Sell both stocks")
} else {
  print("No recommendation")
}

What do you think will print?

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