Author: Lenka Fiřtová

This article describes how to perform the same operation repeatedly, i.e. how to use the for cycle. Cycles save time in a situation when we want to execute the same operation multiple times.

Example 1

Using sample(20), create a randomly ordered sequence of numbers from 1 to 20 called y. Then, using the for cycle, add 20 to all the one-digit numbers in the sequence (so in the end, you should have a sequence of numbers from 10 to 29).

First, let’s create the above-described sequence y and see what it looks like. Since the numbers are ordered randomly, your sequence will look a bit different than the one used here.

Now, let’s use the for cycle, which is going to look like this:

The cycle goes through the numbers in the y sequence one by one and checks whether they have one digit (i.e. whether they are smaller than 10), in which case they are replaced by a number bigger by 20.

The line for(i in 1:20) at the beginning of the for cycle specifies the number of times the cycle should be carried out. In our case, the cycle should be carried out 20 times, i. e. for i ranging from 1 to 20, where i is a “dummy” variable taking on values from 1 to 20. We can use any letter, not necessarily i, provided that it hasn’t yet been used elsewhere in our script yet.

In the curly brackets we specify the operation that should be carried out.

In the first cycle, i takes the value of 1. Subsequently, the operation inside the cycle is carried out. For i = 1 it looks as follows:

It is an if conditional statement which verifies whether the first number in the y sequence (y[1]) is smaller than 10, and if so, it adds 20 to it.

As soon as the first cycle is carried out, i takes the value of 2 and the process is repeated until i is equal to 20.

The final sequence after the whole for cycle has been carried out looks like this:

Example 2

It is possible to use a for cycle inside another one (i.e. use a nested cycle). We will demonstrate it using an example. Let’s create an empty variable z.

Into this variable, we will insert numbers 1, 2, 3, 4 and 5, their second power, third power and fourth power. We can do it this way:

As we are using two for cycles, we need two “dummy” variables, for example i and j (be careful: each for cycle needs to have its own distinctive dummy variable).

The first for cycle says that for i from 1 to 5 we want to carry out a certain operation. The second, nested for cycle says that we want to carry out a certain operation for j from 2 to 4 (as you can see, the for cycle does not need to start with number 1).

By z = c(z, i^j) we state that in the particular iteration of the cycle, we want to add a new number to the z variable: specifically, the j-th power of the number i.

So first, i is set to 1 and number 1 is used in the nested for cycle, where its second, third and fourth powers are taken and added to the z variable. Then, i is set to 2 and again, number 2 is used in the nested for cycle, where its second, third and fourth powers are taken and added to the z variable. This is repeated for i equal to 3, 4 and 5.

The final variable z looks like this:

To conclude, it is important to mention that the “dummy” variables can have different functions, as can be seen from the examples above. If they take values starting from 1 (e.g. i = 1 : 5), then they state which iteration of the cycle is being carried out. But we can also use their specific value (e.g. when j is equal to 3, we can use it the same way we would use number 3). In addition, they can also be used as indexes, i.e. to specify a certain element (for example for i = 2, y[i] would filter out the second element of the y vector).

 

Leave a Reply

Your email address will not be published.

*

clear formPost comment