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.

> y = sample(20)
> y
  [1] 16 11 18 17 10 19 2 12 20 14 8 6 15 4 13 9 7 3 5 1

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

> for(i in 1:20) {
       if(y[i]<10 {
             y[i] = y[i] + 20

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:

> if(y[1]<10) {
        y[1] = y[1]+20  

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:

> y
  [1] 16 11 18 17 10 19 22 12 20 14 28 26 15 24 13 29 27 23 25 21

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.

> z = NULL

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:

> for(i in 1:5) {
      for(j in 2:4)  {
             z = c(z, i^i)

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:

> z
[1] 1  1  1  4  8  16  9  27  81  16  64  256  25  125  625

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