Session 7: Iteration

This session will build on last week’s methods for writing efficient code and reducing needless copy-and-paste through another pillar of programming: iteration. You are likely already familiar with using iteration, perhaps in the form of for or while loops in base R or another programming language. By building and then applying functions in Excel to multiple cells, you have also performed iteration. As with function writing, the goal is not to become fully proficient in the minutiae of iteration but rather to support thinking programmatically about writing efficient code. We will also be relying on iteration in the upcoming statistics sessions.

Extract examples from your previous work Before reading the book chapter, we’d like you to try recalling any examples of how you might have used iteration in your past work, whether in R, MATLAB, Excel or another language. What problem were you attempting to address and how did you use iteration to address it? Be prepared to share these examples in class.

Book chapter reading. Please read Chapter 26 in R for Data Science and then complete the exercises in the chapter.

Tutorials. Go through the r4ds.tutorials: 26-iteration tutorial. Warning: there is a bug in the tutorial in Exercise 2, Save your work. You will need to copy and paste the code block where you initialized gapminder in the previous exercise to get the code to run. Unfortunately, the tutorial does not maintain the object between tabs.

Exercises.

  1. As we will be especially drawing on map() iteration during the statistics training, let’s focus on writing them.
vec <- 1:10
  1. Let’s now consider a basic tibble, dat with three normally distributed random variables, x1, x2 and x3.
dat <- tibble(
  x1 = rnorm(100,mean= 4, sd = 1),
  x2 = rnorm(100, mean = 2,sd = 10),
  x3 = rnorm(100, mean= 8, sd = 2)
)
  1. Finally, come up with two of your own exercises, one involving modifying columns of a tibble (i.e. using across(), if_any() or if_all()) and the other, involving a map function. If you’re feeling adventurous, you could also try experimenting with map2() or integrating your exercises into functions. Like last week, please try embedding them into a Quarto document, using code folding to hide the solutions. Your exercise could also be based on the iterations example from your previous work.

You do not need to send your answers ahead of time but please be prepared to briefly present your exercises during the group meeting next Wednesday (February 4). You do not need to send answers to the r4ds tutorials or book chapter exercises.