R Basics – Conditional Statement and Loops
In the previous part of the R basics course, we learned about data storage options. Now let’s turn to working in R, that is, writing conditional statements and loops in this language.
Conditional statement
The general structure of a conditional statement in R is as follows:
if (condition)
{
action if the condition is met
} else {
action if not met
}
The best way to illustrate how a conditional statement works is to use an example. Let’s first create a variable x that is to contain a value:
x = 10
Now we will write a simple conditional statement that will output the information whether the variable x is a positive number:
if (x › 0 ) print(‘The variable x is positive’)
By executing the line of code entered, we get:
[1] "The variable x is positive"
Let’s add an if else statement to the if statement, which, if the variable x is negative, will show us other information:
if (x › 0)
{
print('The variable x is positive')
} else {
print('The variable x is negative')
}
Let’s execute the statement we entered:
if (x › 0) ...
[1] "The variable x is positive"
Let’s change the value of the variable x to negative:
x = -10
Let’s execute the statement again:
if (x › 0) ...
[1] "The variable x is negative"
If the variable x takes the value 0, we should see a different message than in the two examples above. We can add an additional if else to our statement.
if (x ›= 0)
{
if(x == 0)
{
print(’The variable x is zero')
} else {
print(’The variable x is positive')
}
} else {
print(’The variable x is negative')
}
Let’s change x to zero:
x = 0
And let’s execute the statement:
if (x ›= 0) ...
[1] "The variable x is zero"
FOR LOOP
The general structure of a for loop in R is as follows:
for(i in variable_for_which_the_loop_is_executed)
{
Action for each element of the variable
}
This is what it looks like in practice. Let’s create a vector x with five numbers:
x = c(1:5)
We display the vector x:
x
[1] 1 2 3 4 5
Let’s write a loop that counts the power of each value in the vector x and displays the results of this action:
for (i in x)
{
y = i*i
print(y)
}
Let’s execute the loop:
for (i in x) …
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
The for loop is executed for each element of the vector x.
WHILE LOOP
The general structure of a for while in R is as follows:
while (check)
{
action
}
The loop is executed until the check is true. We will show this using the variable y equal to 0. Let’s write a while loop that displays the result of an action on the variable y. The action will add two to it in the loop until its value is less than 16. The code will look like this:
y = 0
while (y ‹ 16)
{
y = y+2
print(y)
}
Let’s execute the loop:
while (y ‹ 16) …
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
[1] 12
[1] 14
[1] 16
Break i next
The for and while loops can also be broken using break and next. Break is used to stop the loop while next is used to go to the next element. Let’s use the break stop in the previous while loop when y is equal to 8:
y = 0
while (y ‹ 16)
{
y <- y+2
if(y==8) break
print(y)
}
Let’s execute the modified loop:
while (y ‹ 16) …
[1] 2
[1] 4
[1] 6
The loop was stopped when y was equal to 8. Now let’s skip this result in the while loop:
y = 0
while (y ‹ 16)
{
y = y+2
if(y==8) next
print(y)
}
Let’s run the modified loop:
while (y ‹ 16) …
[1] 2
[1] 4
[1] 6
[1] 10
[1] 12
[1] 14
[1] 16
Loops and conditional statements can intertwine to form complex code structures.
The for loop is used in automatic processing of large collections, such as grids. We first code the action on a single file, that is, loading the file, processing it, and saving the result. This action is then closed in a for loop that executes it on our set of files.
In the next course, “R Basics”, we will introduce how to code your own functions.