• 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

For Loop in R Programming

Data Science

R Programming for Data Science For Loop in R Programming

R has three statements that provide explicit looping. They are for, while and repeat. Each of the three statements returns the value of the last statement that was evaluated. R also provides other functions for implicit looping such as tapply, apply, and lapply.

There are two statements that can be used to explicitly control looping. They are break and next. The break statement causes an exit from the innermost loop that is currently being executed. The next statement immediately causes control to return to the start of the loop. The next iteration of the loop (if there is one) is then executed. No statement below next in the current loop is evaluated.

In this lesson, we will discuss the for loop. The syntax of the for loop is:

for ( name in vector )
    statement1

Here vector can be either a vector or a list. For each element in vector the variable name is set to the value of that element and statement1 is evaluated. Basically, the loop iterates over the vector.

Example: Generate Fibonacci sequence for ‘n’ numbers

The following example shows generating Fibonacci sequence for ‘n’ numbers using the For loop.

n <- 10
fib <- numeric(n)
fib[1] <- 1
fib[2] <- 1
for (i in 3:n)
{
  fib[i] <- fib[i-1]+fib[i-2] 
}
print(fib)

The above code will output the Fibonacci series as shown below:

> print(fib)
 [1]  1  1  2  3  5  8 13 21 34 55

Example: Print Stock Prices

Below is another simple example that prints the stocks prices of a stock from a vector containing stock prices for the past five days.

stock_A <- c(10, 8, 9, 11, 12)
for (i in stock_A)
{
  print(i)
}

This will print the stock prices one by one as the loop iterates over the elements in the vector:

[1] 10
[1] 8
[1] 9
[1] 11
[1] 12

Example: Break

The below example demonstrates the use of break statement. For this example, we would like to break and end the loop if the stock price is equal to 11.

stock_A <- c(10, 8, 9, 11, 12)
for (i in stock_A)
{
  if(i == 11)
  {
    break
  } 
  print(i)
}

The loop will execute normally. Each time, it will check if i is equal to 11, if not, it will proceed to print the value of i. If i becomes equal to 11, then the break statement will be executed and the loop will be exited.

[1] 10
[1] 8
[1] 9

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