Learn R Programming

purrr

Overview

purrr enhances R’s functional programming (FP) toolkit by providing a complete and consistent set of tools for working with functions and vectors. If you’ve never heard of FP before, the best place to start is the family of map() functions which allow you to replace many for loops with code that is both more succinct and easier to read. The best place to learn about the map() functions is the iteration chapter in R for Data Science.

Installation

# The easiest way to get purrr is to install the whole tidyverse:
install.packages("tidyverse")

# Alternatively, install just purrr:
install.packages("purrr")

# Or the the development version from GitHub:
# install.packages("pak")
pak::pak("tidyverse/purrr")

Cheatsheet

Usage

The following example uses purrr to solve a fairly realistic problem: split a data frame into pieces, fit a model to each piece, compute the summary, then extract the R2.

library(purrr)

mtcars |> 
  split(mtcars$cyl) |>  # from base R
  map(\(df) lm(mpg ~ wt, data = df)) |> 
  map(summary) |>
  map_dbl("r.squared")
#>         4         6         8 
#> 0.5086326 0.4645102 0.4229655

This example illustrates some of the advantages of purrr functions over the equivalents in base R:

  • The first argument is always the data, so purrr works naturally with the pipe.

  • All purrr functions are type-stable. They always return the advertised output type (map() returns lists; map_dbl() returns double vectors), or they throw an error.

  • All map() functions accept functions (named, anonymous, and lambda), character vector (used to extract components by name), or numeric vectors (used to extract by position).

There are two less obvious advantages:

  • All map() functions have .progress argument so that you can easily track the progress of long running jobs.

  • All map() functions work with in_parallel() to easily spread computation across multiple cores on your computer, or multiple machines over the network.

Copy Link

Version

Install

install.packages('purrr')

Monthly Downloads

941,629

Version

1.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Hadley Wickham

Last Published

July 10th, 2025

Functions in purrr (1.1.0)

as_vector

Coerce a list to a vector
has_element

Does a list contain an object?
head_while

Find head/tail that all satisfies a predicate.
accumulate

Accumulate intermediate results of a vector reduction
along

Create a list of given length
keep

Keep/discard elements based on their values
compose

Compose multiple functions together to create a new function
list_simplify

Simplify a list to an atomic or S3 vector
chuck

Get an element deep within a nested data structure, failing if it doesn't exist
flatten

Flatten a list of lists into a simple vector
list_transpose

Transpose a list
list_flatten

Flatten a list
list_c

Combine list elements into a single data structure
imap

Apply a function to each element of a vector, and its index
in_parallel

Parallelization in purrr
at_depth

Map at depth
get-attr

Infix attribute accessor
keep_at

Keep/discard elements based on their name/position
map_raw

Functions that return raw vectors
modify

Modify elements selectively
modify_tree

Recursively modify a list
modify_in

Modify a pluck location
pluck

Safely get or set an element deep within a nested data structure
%>%

Pipe operator
pluck_depth

Compute the depth of a vector
map_if

Apply a function to each element of a vector conditionally
pmap

Map over multiple input simultaneously (in "parallel")
map_dfr

Functions that return data frames
reexports

Objects exported from other packages
rerun

Re-run expressions multiple times
reduce

Reduce a list to a single value by iteratively applying a binary function
map

Apply a function to each element of a vector
detect

Find the value or position of the first match
lmap

Apply a function to list-elements of a list
insistently

Transform a function to wait then retry after an error
possibly

Wrap a function to return a value instead of an error
array-coercion

Coerce array to list
as_mapper

Convert an object into a mapper function
splice

Splice objects and lists of objects into a list
transpose

Transpose a list.
reduce_right

Reduce from the right (retired)
cross

Produce all combinations of list elements
invoke

Invoke functions.
rbernoulli

Generate random sample from a Bernoulli distribution
prepend

Prepend a vector
rdunif

Generate random sample from a discrete uniform distribution
update_list

Update a list with formulas
when

Match/validate a set of conditions for an object and continue with the action associated with the first valid match.
quietly

Wrap a function to capture side-effects
progress_bars

Progress bars in purrr
purrr_error_indexed

Indexed errors (purrr_error_indexed)
purrr-package

purrr: Functional Programming Tools
attr_getter

Create an attribute getter function
safely

Wrap a function to capture errors
slowly

Wrap a function to wait between executions
auto_browse

Wrap a function so it will automatically browse() on error
faq-adverbs-export

Best practices for exporting adverb-wrapped functions
every

Do every, some, or none of the elements of a list satisfy a predicate?
map2

Map over two inputs
list_assign

Modify a list
map_depth

Map/modify elements at given depth
lift

Lift the domain of a function
partial

Partially apply a function, filling in some arguments
negate

Negate a predicate function so it selects what it previously rejected
rate-helpers

Create delaying rate settings
rate_sleep

Wait for a given time