Chapter 12 Examples using for

12.1 Simple for loop

In this next example, every instance of m is being replaced by each number between 1 and 7, until it reaches the last element of the sequence:

y <- 2
for (m in 1:6) {
    print(y * m)
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
## [1] 12

12.2 for loops on different classes

As expected, you can use for() loops in different object types and classes, such as a list. Let us take the example below, where we are creating the elements object list.

(elements <- list(a = 1:3, b = 4:10, c = 7:-1))
## $a
## [1] 1 2 3
## 
## $b
## [1]  4  5  6  7  8  9 10
## 
## $c
## [1]  7  6  5  4  3  2  1  0 -1

Now, let us print the double of every element of the list:

for (element in elements) {
    print(element * 2)
}
## [1] 2 4 6
## [1]  8 10 12 14 16 18 20
## [1] 14 12 10  8  6  4  2  0 -2

12.3 for and if together

Let us perform operations for even elements within x using the modulo operator (%%):

x <- c(2, 5, 3, 9, 6)
count <- 0
for (val in x) {
    if (val%%2 == 0) {
        count <- count + 1
    }
}
print(count)
## [1] 2

The above example can be represented within the following flowchart:

12.4 for with a real dataset

for() loops are often used to loop over a dataset. We will use loops to perform functions on the CO2 dataset which is built in R. To load and see the first 6 rows of the CO2 dataset, execute the following code:

data(CO2)  # This loads the built in dataset

head(CO2)
##   Plant   Type  Treatment
## 1   Qn1 Quebec nonchilled
## 2   Qn1 Quebec nonchilled
## 3   Qn1 Quebec nonchilled
## 4   Qn1 Quebec nonchilled
## 5   Qn1 Quebec nonchilled
## 6   Qn1 Quebec nonchilled
##   conc uptake
## 1   95   16.0
## 2  175   30.4
## 3  250   34.8
## 4  350   37.2
## 5  500   35.3
## 6  675   39.2

Now, to recursively print the CO2 concentration, let us do this:

for (i in 1:length(CO2[, 1])) {
    # for each row in the CO2 dataset
    print(CO2$conc[i])  # print the CO2 concentration
}

Here are the first 40 outputs:

## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500

Now, let us obtain the CO2 concentration only for the sites that were sampled in Qu├ębec (Canada):

for (i in 1:length(CO2[, 1])) {
    # for each row in the CO2 dataset if the type is
    # 'Quebec'
    if (CO2$Type[i] == "Quebec") {
        print(CO2$conc[i])  # print the CO2 concentration
    }
}
for (i in 1:length(CO2[, 1])) {
    # for each row in the CO2 dataset if the type is
    # 'Quebec'
    if (CO2$Type[i] == "Quebec") {
        print(CO2$conc[i])  # print the CO2 concentration
    }
}

Here they are:

## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000
## [1] 95
## [1] 175
## [1] 250
## [1] 350
## [1] 500
## [1] 675
## [1] 1000

Tip 1. To loop over the number of rows of a data frame, we can use the function nrow():

for (i in 1:nrow(CO2)) {
    # for each row in the CO2 dataset
    print(CO2$conc[i])
    # print the CO2 concentration
}

Tip 2. To perform operations on the elements of one column, we can directly iterate over it.

for (p in CO2$conc) {
    # for each element of the column 'conc' of the CO2 df
    print(p)
    # print the p-th element
}

Tip 3. The expression within the loop can be almost anything and is usually a compound statement containing many commands.

for (i in 4:5) {
    # for i in 4 to 5
    print(colnames(CO2)[i])
    print(mean(CO2[, i]))  # print the mean of that column from the CO2 dataset
}