Stop repeating yourself: functions and iteration in R

Published

June 18, 2026

Description

Sooner or later most R users need to do the same thing many times, like run an analysis for each species, read in every file in a folder, fit a model to each treatment group. Writing functions is one of the more useful habits in day-to-day data analysis, and this workshop covers the two tools that handle repetition cleanly:

  • Writing your own functions: wrapping repeated code into reusable functions with arguments, defaults, and return values
  • Iterating with the purrr package: using map() and friends to apply a function across many inputs, replacing copy-paste or for-loops

We will work through practical examples together and you will learn how to take repetitive code of your own and refactor it into a function you apply across many inputs.

Slides in full screen     Download PDF slides

Resources

Workshop materials

The workshop repo has all demo scripts, exercises, and solutions:

github.com/selinaZitrone/functions-iteration-workshop

Writing functions

Iteration with purrr

More purrr tools: