Reader small image

You're reading from  Learning Quantitative Finance with R

Product typeBook
Published inMar 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781786462411
Edition1st Edition
Languages
Right arrow
Authors (2):
Dr. Param Jeet
Dr. Param Jeet
author image
Dr. Param Jeet

Dr. Param Jeet is a Ph.D. in mathematics from one of India's leading technological institute in Madras (IITM), India. Dr. Param Jeet has a couple of mathematical research papers published in various international journals. Dr. Param Jeet has been into the analytics industry for the last few years and has worked with various leading multinational companies as well as consulted few of companies as a data scientist.
Read more about Dr. Param Jeet

PRASHANT VATS
PRASHANT VATS
author image
PRASHANT VATS

Prashant Vats is a masters in mathematics from one of India's leading technological institute, IIT Mumbai. Prashant has been into analytics industry for more than 10 years and has worked with various leading multinational companies as well as consulted few of companies as data scientist across several domain.
Read more about PRASHANT VATS

View More author details
Right arrow

Loops (for, while, if, and if...else)


Loops are instructions for automating a multistep process by organizing sequences of actions by grouping the parts which need to be repeated. All the programming languages come up with built-in constructs, which allow the repetition of instructions or blocks of instructions. In programming languages, there are two types of loops.

Decision-making is one of the significant components of programming languages. This can be achieved in R programming by using the conditional statement if...else. The syntax, along with an example, is given here.

Let us first discuss if and else conditional statements and then we will discuss loops.

if statement

Let us first see how if and else work in R. The general syntax for an if clause is given here:

if (expression) { 
   statement 
} 

If an expression is correct then the statement gets executed else nothing happens. An expression can be a logical or numeric vector. In the case of numeric vectors, 0 is taken as False and the rest are taken as True, for example:

>x<-5 
>if(x>0) 
>+ { 
>+ print(" I am Positive") 
>+ } 

When the preceding code gets executed then it prints I am Positive.

if...else statement

Now let us see how the if and else conditions work in R. Here is the syntax:

if(expression){ 
   statement1 
} else { 
   statement2 
} 

The else part is evaluated in case if the if part is False, for example:

> x<--5 
> if(x>0) 
>+ { 
>+ print(" I am Positive") 
>+ }else 
>+{ 
>+ print(" I am Negative") 
>+} 

When the preceding code gets executed, it prints I am Negative.

for loop

These loops are executed for a defined number of times and are controlled by a counter or index and incremented at each cycle. Please find here the syntax of the for loop construct:

for (val in sequence) { 
    statement 
} 

Here is an example:

>Var <- c(3,6,8,9,11,16) 
>counter <- 0 
>for (val in Var) { 
>+    if(val %% 2 != 0)  counter = counter+1 
>+} 
print(counter) 

When the preceding code gets executed, it counts the number of odd numbers present in vector c, that is, 3.

while loop

while loops are the loops which are set at onset for verifying the logical condition. The logical condition is tested at the start of the loop construct. Here is the syntax:

while (expression) { 
   statement 
} 

Here, the expression is evaluated first and, if it is true, the body of the for loop gets executed. Here is an example:

>Var <- c("Hello") 
>counter <- 4 
>while (counter < 7) { 
>+   print(Var) 
>+   counter = counter+ 1 
>+} 

Here, first the expression gets evaluated and, if it is true, the body of the loop gets executed and it keeps executing till the expression returns False.

apply()

apply() is a function in R used for quick operations on a matrix, vector, or array and can be executed on rows, columns, and on both together. Now let us try to find the sum of rows of a matrix using the apply function. Let us execute the following code:

> sample = matrix(c(1:10), nrow = 5 , ncol = 2) 
> apply(sample, 1,sum) 

It generates the sum row-wise.

sapply()

sapply() operates over a set of data such as a list or vector, and calls the specified function for each item. Let us execute the following code to check the example:

> sapply(1:5, function(x) x^3) 

It computes cubes for 1 to 5.

Previous PageNext Page
You have been reading a chapter from
Learning Quantitative Finance with R
Published in: Mar 2017Publisher: PacktISBN-13: 9781786462411
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Dr. Param Jeet

Dr. Param Jeet is a Ph.D. in mathematics from one of India's leading technological institute in Madras (IITM), India. Dr. Param Jeet has a couple of mathematical research papers published in various international journals. Dr. Param Jeet has been into the analytics industry for the last few years and has worked with various leading multinational companies as well as consulted few of companies as a data scientist.
Read more about Dr. Param Jeet

author image
PRASHANT VATS

Prashant Vats is a masters in mathematics from one of India's leading technological institute, IIT Mumbai. Prashant has been into analytics industry for more than 10 years and has worked with various leading multinational companies as well as consulted few of companies as data scientist across several domain.
Read more about PRASHANT VATS