R has a set of control structures that organize the flow of execution of a program, depending on the conditions of the environment. Here are the most important ones:
- If/else: This can test a condition and execute it accordingly
- for: Executes a loop that repeats for a certain number of times, as defined in the code
- while: This evaluates a condition and executes only until the condition is true
- repeat: Executes a loop an infinite number of times
- break: Used to interrupt the execution of a loop
- next: Used to jump through similar iterations to decrease the number of iterations and time taken to get the output from the loop
- return: Abandons a function
The structure of if else is as if (test_expression) { statement }.
Here, if the test_expression returns true, the statement will execute; otherwise, it won't.
An additional else condition can be added like if (test_expression) { statement1 } else { statement2 }.
In this case, the else condition is executed only if test_expression returns false.
Let's see how this works. We will evaluate an if expression like so:
x<-4
y<-3
if (x >3) {
y <- 10
} else {
y<- 0
}
Since x takes a value higher than 3, then the y value should be modified to take a value of 10:
print(y)
## [1] 10
If there are more than two if statements, the else expression is transformed into else if like this if ( test_expression1) { statement1 } else if ( test_expression2) { statement2 } else if ( test_expression3) { statement3 } else { statement4 }.
The for command takes an iterator variable and assigns its successive values of a sequence or vector. It is usually used to iterate on the elements of an object, such as vector lists.
An easy example is as follows, where the i variable takes different values from 1 to 10 and prints them. Then, the loop finishes:
for (i in 1:10){
print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
Additionally, loops can be nested in the same code:
x<- matrix(1:6,2,3)
for (i in seq_len(nrow(x))){
for (j in seq_len(ncol(x))){
print(x[i,j])}
}
## [1] 1
## [1] 3
## [1] 5
## [1] 2
## [1] 4
## [1] 6
The while command is used to create loops until a specific condition is met. Let's look at an example:
x <- 1
while (x >= 1 & x < 20){
print(x)
x = x+1
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15
## [1] 16
## [1] 17
## [1] 18
## [1] 19
Here, values of x are printed, while x takes higher values than 1 and less than 20. While loops start by testing the value of a condition, if true, the body of the loop is executed. After it has been executed, it will test the condition again, and keep on testing it until the result is false.
The repeat and break commands are related. The repeat command starts an infinite loop, so the only way out of it is through the break instruction:
x <- 1
repeat{
print(x)
x = x+1
if (x == 6){
break
}
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
We can use the break statement inside for and while loops to stop the iterations of the loop and control the flow.
Finally, the next command can be used to skip some iterations without getting them terminated. When the R parser reads next, it terminates the current iteration and moves on to another new iteration.
Let's look at an example of next, where 20 iterations are skipped:
for (i in 1:15){
if (i <= 5){
next
} else { print(i)
} }
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15
Before we start the next chapters of this book, it is recommended to practice these codes. Take your time and think about the code and how to use it. In the upcoming chapters, you will see a lot of code and new functions. Don't be concerned if you don't understand all of them. It is more important to have an understanding of the entire process to develop a predictive model and all the things you can do with R.
I have tried to make all of the code accessible, and it is possible to replicate all the tables and results provided in this book. Just enjoy understanding the process and reuse all the code you need in your own applications.