# Functional Programming with {purrr}

Course Level: Foundation  
_(6 hours)_

This is a one-day course on the {tidyverse} package, {purrr}. {purrr} is a very powerful package that gives great flexibility to analysts, by enhancing R’s functional programming toolkit. We will demonstrate how to use functions such as `map()`, `map2()` and `pmap()`, to iteratively map functions over multi-element objects like vectors and lists. Emphasis will also be placed on how we can manipulate list outputs and how this can be applied to our data.

[Download course details](/content/training/course/r-tidyverse-programming-purrr-lists/assets/course-outline.pdf)

## Course Details

### Outline

- **Introduction to {purrr} and Lists:** Introduction to lists in R and using {purrr} to map a function across a list.
- **List-Columns and Nesting:** Exploring nested data in list columns and using the mapping functions to manipulate them.
- **Parallel Mapping:** Using {purrr} functions to map over multiple lists in parallel.
- **Manipulating {purrr} Output:** Using {purrr} to efficiently extract elements from lists into vector and dataframe format, and change the hierarchy within nested lists.
- **Best Practices in {purrr}:** Showcase of functions from {purrr} which aid in the debugging process.

### Learning outcomes

**Session 1:**

_By the end of session 1, participants will be able to…_

- understand lists in R and know how to use {purrr} to map functions.
- know what nested loops are and use {magrittr} to extract elements from them.
- be able to create list columns and know how to access the data in them.

**Session 2:**

_By the end of session 2, participants will be able to…_

- iteratively loop two or more objects to a function of choice using functions such as `map2()`, `pmap()` and `imap()`.
- recognize the advantages of using {purrr}.
- understand how to extract elements from nested lists to achieve a desired output object class.
- be able to effectively debug their code using multiple {purrr} functions for the debugging process.
- save precious debugging time using e.g. `safely()`

_This course does not include:_

- The {stringr} package, also from the {tidyverse} which helps with splitting and combining strings, manipulating text data and working with regular expressions. Jumping Rivers run a [Text Mining in R](/content/training/course/r-text-mining-tidyverse-stringr-tidytext/index.html) course which covers {stringr} in detail.

No in-depth coverage of the tidyverse [data visualisation package {ggplot2}](/content/training/course/r-advanced-graphics-ggplot2-plotly-themes-scaling-faceting/index.html), see our course on this for more information.

### Prior knowledge

This course assumes basic Tidyverse skills, so having attended [Data Wrangling in the Tidyverse](/content/training/course/data-tidyverse-dplyr-tidyr-lubridate-forcats/index.html) or equivalent is necessary. As this course involves iterative programming, basic familiarity with for loops may be helpful. You can learn about for loops on our [Programming with R](/content/training/course/r-programming-functions-looping-conditionals/index.html) course.

## Attendee Feedback

- “Dealing with lists is something I’ve always struggled with, but this course has given me so much more confidence in handling and manipulating them!”
