- Home
- Training Library
- Programming
- Courses
- 8. Conditional Statements in R

# 8. Conditional Statements in R

## Contents

###### Fundamentals of R

## The course is part of this learning path

**Difficulty**Intermediate

**Duration**33m

**Students**10

**Ratings**

### Description

This module looks at conditional statements in R, such as for loops and how to repeat functions.

**Learning Objectives**

The objectives of this module are to provide you with an understanding of:

- When to use a for loop in R
- How to nest a for loop
- Built-in functions being vectorized
- How to apply functions
- How to use the family of apply functions

**Intended Audience**

Aimed at anyone who wishes to learn the R programming language.

**Prerequisites**

No prior knowledge of R is assumed. You should already be familiar with basic programming concepts such as variables, scope, and functions. Experience of another scripting language such as Python or Perl would be an advantage. An understanding of mathematical concepts would be beneficial.

**Feedback**

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think.

### Transcript

When we need to explicitly repeat an operation, we might choose to use the for loop. The for loop is only useful when the order of iteration is important meaning that a calculation at each iteration depends on the results of the previous iteration. If this is not the case, then we might, and we probably would, utilize the power of R and vectorization to implicitly repeat operations. With respect to a for loop, the structure is as follows. We have a for as the outer statement, we have an iterator that runs over a set of values inside a pair of brackets, and inside a pair of curly brackets, we have a code block which includes things to be done. As a simple example, we might print the numbers two and three to the screen. The print function is the thing that we are doing. The set of values that we are iterating over are two and three. The iterator in this case is i, and we are using the i at every iteration to be printed. Instead of using a vector of two to three, I could've chosen to use a vector ranging from two through 'til three, or five in this case. I could change the i to be anything. Here I chose to use the four-character word I-T-E-R. I could've also changed the length of the vector with which we are iterating over. I can also have multiple lines inside my code block. Here I choose to multiply the iterator by two and then print it to the screen after assigning it to a value K-H and as we can see in our global environment after having run this function, or having run this for loop, we'll see that the last value of K-H is stored in the global environment.

### Lectures

# About the Author

**Students**285

**Labs**1

**Courses**11

**Learning paths**1

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum.

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation.

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios.

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform.

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data.

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.