Introduction to the `purrr` Package in R
Welcome to the world of functional programming in R with the
purrr
purrr
What is Functional Programming?
Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. In essence, it emphasizes using functions as first-class citizens, meaning functions can be passed as arguments to other functions, returned as values from other functions, and assigned to variables. This leads to code that is often more declarative, predictable, and easier to reason about.
`purrr` simplifies applying functions to collections of data.
Instead of writing explicit loops, purrr
provides functions like map()
that apply a given function to each element of a list or vector, returning a new list.
The core idea behind purrr
is to abstract away the common patterns of iteration. Think about a task where you need to perform the same operation on every item in a list, like calculating the mean of each sub-list or transforming each string. Traditionally, you might use a for
loop. purrr
's map()
family of functions provides a more elegant and often more efficient way to achieve this. For example, map(list_of_numbers, mean)
will calculate the mean for each element in list_of_numbers
.
The `map()` Family of Functions
The
purrr
map()
map()
map_dbl()
map_chr()
map_int()
map_lgl()
Function | Purpose | Output Type |
---|---|---|
map() | Apply a function to each element of a list/vector. | List |
map_dbl() | Apply a function and ensure the output is a double vector. | Double Vector |
map_chr() | Apply a function and ensure the output is a character vector. | Character Vector |
map_int() | Apply a function and ensure the output is an integer vector. | Integer Vector |
map_lgl() | Apply a function and ensure the output is a logical vector. | Logical Vector |
Using `.x` and `.f`
A common pattern in
purrr
.x
.f
~ .x + 1
purrr
's map()
functions over traditional for
loops for repetitive operations on collections?They provide a more concise, readable, and often more efficient way to apply functions to each element of a list or vector, abstracting away explicit loop management.
Beyond `map()`: `walk()`, `reduce()`, and `map2()`
purrr
map()
walk()
map()
reduce()
map2()
pmap()
Imagine you have a list of data frames, and you want to calculate the mean of a specific column in each data frame. Without purrr
, you might write a loop like this:
results <- list()
for (i in seq_along(list_of_dfs)) {
results[[i]] <- mean(list_of_dfs[[i]]$column_name)
}
With purrr
, this becomes much cleaner:
results <- map(list_of_dfs, ~ mean(.x$column_name))
Or, if you know you want a numeric vector as output:
results <- map_dbl(list_of_dfs, ~ mean(.x$column_name))
This illustrates how purrr
functions abstract the iteration process, making the code more declarative and easier to understand. The ~
syntax creates an anonymous function, where .x
represents the current element being processed (in this case, a data frame from list_of_dfs
).
Text-based content
Library pages focus on text content
Key Takeaways
By embracing
purrr
map()
Learning Resources
The official documentation for the purrr package, providing a comprehensive overview of its functions and concepts.
A chapter from the highly acclaimed 'R for Data Science' book, explaining purrr and functional programming in an accessible way.
A practical tutorial with clear examples demonstrating how to use the core purrr functions for data manipulation.
A video tutorial that walks through the basics of purrr and its benefits for R users.
A handy cheat sheet summarizing the most common purrr functions and their usage.
A blog post offering a more in-depth exploration of purrr, including advanced techniques and use cases.
Guidance on how to write purrr code that aligns with the tidyverse style conventions.
Explains the underlying principles of functional programming and how they are applied within R, with a focus on purrr.
A focused look at the different `map()` functions in purrr and when to use each one.
A lecture from a Coursera course that introduces the purrr package as part of a broader R programming curriculum.