# Control Flow

## Overview

Teaching: 45 min
Exercises: 20 min
Questions
• How can I make data-dependent choices in R?

• How can I repeat operations in R?

Objectives
• Write conditional statements with `if()` and `else()`.

• Write and understand `for()` loops.

Often when we’re coding we want to control the flow of our actions. This can be done by setting actions to occur only if a condition or a set of conditions are met. Alternatively, we can also set an action to occur a particular number of times.

There are several ways you can control flow in R. For conditional statements, the most commonly used approaches are the constructs:

``````# if
if (condition is true) {
perform action
}

# if ... else
if (condition is true) {
perform action
} else {  # that is, if the condition is false,
perform alternative action
}
``````

Say, for example, that we want R to print a message if a variable `x` has a particular value:

``````x <- 11

if (x >= 10) {
print("x is greater than or equal to 10")
}
``````
``````##  "x is greater than or equal to 10"
``````

``````x <- 6

if (x >= 10) {
print("x is greater than or equal to 10")
} else if (x > 5) {
print("x is greater than 5")
} else {
print("x is less than 5")
}
``````
``````##  "x is greater than 5"
``````

Important: when R evaluates the condition inside `if()` statements, it is looking for a logical element, i.e., `TRUE` or `FALSE`. This can cause some headaches for beginners. For example:

``````x  <-  4 == 3
x
``````
``````##  FALSE
``````
``````if (x) {
"4 equals 3"
}
``````

As we can see, the message was not printed because the vector x is `FALSE`

## Challenge 1

Use an `if()` statement to print a suitable message reporting whether there are any records from 2002 in the `gapminder` dataset. Now do the same for 2012.

## Solution to Challenge 1

We will first see a solution to Challenge 1 which does not use the `any()` function. We first obtain a logical vector describing which element of `gapminder\$year` is equal to `2002`:

``````gapminder[(gapminder\$year == 2002),]
``````

Then, we count the number of rows of the data.frame `gapminder` that correspond to the 2002:

``````rows2002_number <- nrow(gapminder[(gapminder\$year == 2002),])
``````

The presence of any record for the year 2002 is equivalent to the request that `rows2002_number` is one or more:

``````rows2002_number >= 1
``````

Putting all together, we obtain:

``````if(nrow(gapminder[(gapminder\$year == 2002),]) >= 1){
print("Record(s) for the year 2002 found.")
}
``````

All this can be done more quickly with `any()`. The logical condition can be expressed as:

``````if(any(gapminder\$year == 2002)){
print("Record(s) for the year 2002 found.")
}
``````

Did anyone get a warning message like this?

``````## Warning in if (gapminder\$year == 2012) {: the condition has length > 1 and
## only the first element will be used
``````

If your condition evaluates to a vector with more than one logical element, the function `if()` will still run, but will only evaluate the condition in the first element. Here you need to make sure your condition is of length 1.

## Tip: `any()` and `all()`

The `any()` function will return TRUE if at least one TRUE value is found within a vector, otherwise it will return `FALSE`. This can be used in a similar way to the `%in%` operator. The function `all()`, as the name suggests, will only return `TRUE` if all values in the vector are `TRUE`.

## Repeating operations

If you want to iterate over a set of values, when the order of iteration is important, and perform the same operation on each, a `for()` loop will do the job. We saw `for()` loops in the shell lessons earlier. This is the most flexible of looping operations, but therefore also the hardest to use correctly. Avoid using `for()` loops unless the order of iteration is important: i.e. the calculation at each iteration depends on the results of previous iterations.

The basic structure of a `for()` loop is:

``````for(iterator in set of values){
do a thing
}
``````

For example:

``````for(i in 1:10){
print(i)
}
``````
``````##  1
##  2
##  3
##  4
##  5
##  6
##  7
##  8
##  9
##  10
``````

The `1:10` bit creates a vector on the fly; you can iterate over any other vector as well.

We can use a `for()` loop nested within another `for()` loop to iterate over two things at once.

``````for(i in 1:5){
for(j in c('a', 'b', 'c', 'd', 'e')){
print(paste(i,j))
}
}
``````
``````##  "1 a"
##  "1 b"
##  "1 c"
##  "1 d"
##  "1 e"
##  "2 a"
##  "2 b"
##  "2 c"
##  "2 d"
##  "2 e"
##  "3 a"
##  "3 b"
##  "3 c"
##  "3 d"
##  "3 e"
##  "4 a"
##  "4 b"
##  "4 c"
##  "4 d"
##  "4 e"
##  "5 a"
##  "5 b"
##  "5 c"
##  "5 d"
##  "5 e"
``````

Rather than printing the results, we could write the loop output to a new object.

``````output_vector <- c()
for(i in 1:5){
for(j in c('a', 'b', 'c', 'd', 'e')){
temp_output <- paste(i, j)
output_vector <- c(output_vector, temp_output)
}
}
output_vector
``````
``````##   "1 a" "1 b" "1 c" "1 d" "1 e" "2 a" "2 b" "2 c" "2 d" "2 e" "3 a"
##  "3 b" "3 c" "3 d" "3 e" "4 a" "4 b" "4 c" "4 d" "4 e" "5 a" "5 b"
##  "5 c" "5 d" "5 e"
``````

This approach can be useful, but ‘growing your results’ (building the result object incrementally) is computationally inefficient, so avoid it when you are iterating through a lot of values.

## Tip: don’t grow your results

One of the biggest things that trips up novices and experienced R users alike, is building a results object (vector, list, matrix, data frame) as your for loop progresses. Computers are very bad at handling this, so your calculations can very quickly slow to a crawl. It’s much better to define an empty results object before hand of the appropriate dimensions. So if you know the end result will be stored in a matrix like above, create an empty matrix with 5 row and 5 columns, then at each iteration store the results in the appropriate location.

## Tip: While loops

Sometimes you will find yourself needing to repeat an operation until a certain condition is met. You can do this with a `while()` loop.

``````while(this condition is true){
do a thing
}
``````

As an example, here’s a while loop that generates random numbers from a uniform distribution (the `runif()` function) between 0 and 1 until it gets one that’s less than 0.1.

``````z <- 1
while(z > 0.1){
z <- runif(1)
print(z)
}
``````

`while()` loops will not always be appropriate. You have to be particularly careful that you don’t end up in an infinite loop because your condition is never met.

## Challenge 2

Write a script that loops through the `gapminder` data by continent and prints out whether the mean life expectancy is smaller or larger than 50 years. Hints: functions to use are `unique()` and `mean()`. Use the help function to figure out how these functions will be useful for this challenge.

## Solution to Challenge 2

Step 1: We want to make sure we can extract all the unique values of the continent vector

``````gapminder <- read.csv("data/gapminder.csv")
unique(gapminder\$continent)
``````

Step 2: We also need to loop over each of these continents and calculate the average life expectancy for each `subset` of data. We can do that as follows:

1. Loop over each of the unique values of ‘continent’
2. For each value of continent, create a temporary variable storing the life exepectancy for that subset,
3. Return the calculated life expectancy to the user by printing the output:
``````for( iContinent in unique(gapminder\$continent) ){
tmp <- mean(subset(gapminder, continent==iContinent)\$lifeExp)
cat("Average Life Expectancy in", iContinent, "is", tmp, "\n")
rm(tmp)
}
``````

Step 3: The exercise only wants the output printed if the average life expectancy is less than 50 or greater than 50. So we need to add an `if` condition before printing. So we need to add an `if` condition before printing, which evaluates whether the calculated average life expectancy is above or below a threshold, and print an output conditional on the result. We need to amend (3) from above:

3a. If the calculated life expectancy is less than some threshold (50 years), return the continent and a statement that life expectancy is less than threshold, otherwise return the continent and a statement that life expectancy is greater than threshold,:

``````thresholdValue <- 50

for( iContinent in unique(gapminder\$continent) ){
tmp <- mean(subset(gapminder, continent==iContinent)\$lifeExp)

if(tmp < thresholdValue){
cat("Average Life Expectancy in", iContinent, "is less than", thresholdValue, "\n")
}
else{
cat("Average Life Expectancy in", iContinent, "is greater than", thresholdValue, "\n")
} # end if else condition
rm(tmp)
} # end for loop
``````

## Challenge 3

Modify the script from Challenge 4 to loop over each country. This time print out whether the life expectancy is smaller than 50, between 50 and 70, or greater than 70.

## Solution to Challenge 3

We modify our solution to Challenge 3 by now adding two thresholds, `lowerThreshold` and `upperThreshold` and extending our if-else statements:

`````` lowerThreshold <- 50
upperThreshold <- 70

for( iCountry in unique(gapminder\$country) ){
tmp <- mean(subset(gapminder, country==iCountry)\$lifeExp)

if(tmp < lowerThreshold){
cat("Average Life Expectancy in", iCountry, "is less than", lowerThreshold, "\n")
}
else if(tmp > lowerThreshold && tmp < upperThreshold){
cat("Average Life Expectancy in", iCountry, "is between", lowerThreshold, "and", upperThreshold, "\n")
}
else{
cat("Average Life Expectancy in", iCountry, "is greater than", upperThreshold, "\n")
}
rm(tmp)
}
``````

Write a script that loops over each country in the `gapminder` dataset, tests whether the country starts with a ‘B’, and graphs life expectancy against time as a line graph if the mean life expectancy is under 50 years.

Solution for Challenge 5

We will use the `grep` command that was introduced in the Unix Shell lesson to find countries that start with “B.” Lets understand how to do this first. Following from the Unix shell section we may be tempted to try the following

``````grep("^B", unique(gapminder\$country))
``````

But when we evaluate this command it returns the indices of the factor variable `country` that start with “B.” To get the values, we must add the `value=TRUE` option to the `grep` command:

``````grep("^B", unique(gapminder\$country), value=TRUE)
``````

We will now store these countries in a variable called candidateCountries, and then loop over each entry in the variable. Inside the loop, we evaluate the average life expectancy for each country, and if the average life expectancy is less than 50 we use base-plot to plot the evolution of average life expectancy:

``````candidateCountries <- grep("^B", unique(gapminder\$country), value=TRUE)

for( iCountry in candidateCountries){
tmp <- mean(subset(gapminder, country==iCountry)\$lifeExp)

if(tmp < thresholdValue){
cat("Average Life Expectancy in", iCountry, "is less than", thresholdValue, "plotting life expectancy graph... \n")

with(subset(gapminder, country==iCountry),
plot(year,lifeExp,
type="o",
main = paste("Life Expectancy in", iCountry, "over time"),
ylab = "Life Expectancy",
xlab = "Year"
) # end plot
) # end with
} # end for loop
rm(tmp)
}
``````

## Key Points

• Use `if` and `else` to make choices.

• Use `for` to repeat operations.