12.1 Introduction

To “re-iterate” something is to repeat something. In ordinary language, proceeding in an “iterative” fashion typically means to proceed step-by-step. In computer science, the notion of iteration is an essential concept that implies that some code is executed repeatedly. However, doing exactly the same thing more than once only makes sense if some aspect of the computed code or answer can change. Thus, when repeating some code there typically is a value or object (e.g., some variable or data) that changes between consecutive iterations.

Whereas people easily get bored when doing the same thing over and over again, computers usually do not mind and are really good at it. Thus, whenever a procedure or process requires many repetitions, computers really can shine.

12.1.1 Objectives

After working through this chapter, you should be able to:

  1. understand the notion of iteration in computer programming,
  2. use for loops to iterate over sequences of a known length,
  3. use while loops to iterate over sequences of an unknown length,
  4. use base R apply functions to replace some loops,
  5. use purrr map functions to replace some loops.

12.1.2 Loops vs. functions

The traditional way to address issues of iteration is by repeating code in for loops. As R is a functional programming language, it provides additional ways of applying functions repeatedly to data structures. As we focus on the essentials in this book, we will primarily focus on for and while loops, and only briefly introduce the base R apply (R Core Team, 2019) and purrr’s map family of functions (Henry & Wickham, 2019).

12.1.3 Data used

To illustrate the notion of iteration in various type of loops, we are using various toy datasets (from the ds4psy package) in this chapter. For instance, the dataset tb includes some information on 100 ficticious people and pi_100k contains the first 100.000 digits of pi.

12.1.4 Getting ready

This chapter formerly assumed that you have read and worked through Chapter 21: Iteration of the r4ds textbook (Wickham & Grolemund, 2017). It now can be read by itself, but reading Chapter 21 of r4ds is still recommended.

Please do the following to get started:

  • Create an R Markdown (.Rmd) document (for instructions, see Appendix E and the templates linked in Section E.2).

  • Structure your document by inserting headings and empty lines between different parts. Here’s an example how your initial file could look:

---
title: "Chapter 12: Iteration"
author: "Your name"
date: "2020 February 10"
output: html_document
---

Add text or code chunks here.

# Exercises (WPA12)
  
## Exercise 1
  
## Exercise 2
  
etc. 
  
<!-- The end (eof). -->
  • Create an initial code chunk below the header of your .Rmd file that loads the R packages of the tidyverse (and see Section E.3.3 if you want to get rid of the messages and warnings of this chunk in your HTML output).

  • Save your file (e.g., as 12_iteration.Rmd in the R folder of your current project) and remember saving and knitting it regularly as you keep adding content to it.

References

Henry, L., & Wickham, H. (2019). purrr: Functional programming tools. Retrieved from https://CRAN.R-project.org/package=purrr

R Core Team. (2019). R: A language and environment for statistical computing. Retrieved from https://www.R-project.org

Wickham, H., & Grolemund, G. (2017). R for data science: Import, tidy, transform, visualize, and model data. Retrieved from http://r4ds.had.co.nz