Learning Objectives

  • Write and use for loops to apply the same code to multiple objects or elements
  • Use if-else statements to conditionally execute code

Required Packages and Datasets

Make sure you can load all these packages and dataset before starting the module

library(dplyr)
library(uwpols501)
data(iver)

For Loops

We use for loops to iterate through multiple elements of a list, variable, etc., and do something with those elements.

A for loop has the following skeleton:

for (each_element in here) { # Try to use meaningful names for the
  # do something             #   "each_element" object
  print(each_element)
}

A short/silly example:

list_numbers <- sample(x = 1:100, size = 10, replace = FALSE) # sample() function
for (number in list_numbers) {
  print(paste0("2 times ", number, " is ", number * 2))
}
## [1] "2 times 56 is 112"
## [1] "2 times 6 is 12"
## [1] "2 times 46 is 92"
## [1] "2 times 47 is 94"
## [1] "2 times 78 is 156"
## [1] "2 times 36 is 72"
## [1] "2 times 52 is 104"
## [1] "2 times 16 is 32"
## [1] "2 times 58 is 116"
## [1] "2 times 81 is 162"

A real example:

Imagine that we want to calculate the \(log\) of the variables povred and enp of the iver dataset. We can create a list with these two variable names and then, for each of them, find them in iver, calculate the \(log\), and add the result as a new variable to the dataset.

numeric_vars <- c("povred", "enp")
for (variable in numeric_vars) {
  y <- iver[,variable]
  log_y <- log(y)
  name_new_var <- paste0(variable, "_log")
  iver[,name_new_var] <- log_y
}

Conditional Execution: if and else statements

Sometimes we only want to execute certain code if the data fulfills some conditions. To do that we use if and else statements.

How if and else statements look like:

if (this) {
  # do that
} else if (that) {
  # do something else
} else {
  #
}

This and that in the previous chunk of code are boolean tests: code that returns TRUE/FALSE when the computer executes it. Some examples of boolean tests:

10 == 2
## [1] FALSE
10 %in% c(2, 5, 13, 20, 10)
## [1] TRUE
10 == 2 & 10 %in% c(2, 5, 13, 20, 10)
## [1] FALSE
10 == 2 | 10 %in% c(2, 5, 13, 20, 10)
## [1] TRUE

A short/silly example:

some_numbers <- c(1, 4, 6, 10,12, 16, 45, 88, 102)
for (number in some_numbers) {
  if (number < 10) {
    print(paste0(number, " is smaller than 10"))
  } else if (number < 50) {
    print(paste0(number, " is smaller than 50 but greater or equal to 10"))
  } else {
    print(paste0(number, " is greater than 50"))
  }
}
## [1] "1 is smaller than 10"
## [1] "4 is smaller than 10"
## [1] "6 is smaller than 10"
## [1] "10 is smaller than 50 but greater or equal to 10"
## [1] "12 is smaller than 50 but greater or equal to 10"
## [1] "16 is smaller than 50 but greater or equal to 10"
## [1] "45 is smaller than 50 but greater or equal to 10"
## [1] "88 is greater than 50"
## [1] "102 is greater than 50"

Real example:

We previously calculated the \(log\) and added it to the dataset for the numeric variables in iver. To do that we first created a list with the numeric variables. This time we won’t create that list. Instead, we’ll use conditional execution to indicate that we only want to take the \(log\) of numeric variables.

data(iver) # load the data set again
iver <- as.data.frame(iver)
for (variable in names(iver)) {
  y <- iver[,variable]
  if (is.numeric(y)) {
    log_y <- log(y)
    name_new_var <- paste0(variable, "_log")
    iver[,name_new_var] <- log_y
  } else{
    print(paste0(variable, " is not a numeric variable."))
  }
}
## [1] "cty is not a numeric variable."
## [1] "elec_sys is not a numeric variable."