Functions and Iteration in R

Author

Jason Struck

Published

August 13, 2024

At this point in your R language acquisition, you are able to write scripts that perform tasks like cleaning a dataset, fitting a series of models, producicing plots for residual diagnostics, and even simulating datasets.

(If you are new to R, see our Introduction to R and Data Wrangling with R materials.)

A common problem at this stage is that we are are copying and pasting code to perform repetitive tasks like

The primary issues with the copy-paste approach to repetitive tasks are

  1. Scaling up: It is fine if we need to make 5 graphs, but not 50.
  2. Updating: Changing one thing in a process we repeat n times requires changing it n times. It may be a typo we missed earlier, or it may be that we want to change something like the color of a line or the number of decimal places. This is tedious and makes room for mistakes.
  3. Organization: Scripts can quickly become unwieldy and difficult to navigate and maintain when they have repeated code.

Functions and iteration solve these issues by making procedures compact and portable (functions) and allowing us to repeat those procedures across sets of objects (iteration).