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

1,618,262

Version

1.2.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Hadley Wickham

Last Published

November 4th, 2025

Functions in purrr (1.2.0)

has_element

Does a list contain an object?
every

Do every, some, or none of the elements of a list satisfy a predicate?
faq-adverbs-export

Best practices for exporting adverb-wrapped functions
imap

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

Find the value or position of the first match
in_parallel

Parallelization in purrr
insistently

Transform a function to wait then retry after an error
invoke

Invoke functions.
flatten

Flatten a list of lists into a simple vector
head_while

Find head/tail that all satisfies a predicate.
map

Apply a function to each element of a vector
list_c

Combine list elements into a single data structure
lmap

Apply a function to list-elements of a list
list_flatten

Flatten a list
keep

Keep/discard elements based on their values
list_assign

Modify a list
list_transpose

Transpose a list
lift

Lift the domain of a function
keep_at

Keep/discard elements based on their name/position
list_simplify

Simplify a list to an atomic or S3 vector
map_dfr

Functions that return data frames
partial

Partially apply a function, filling in some arguments
negate

Negate a predicate function so it selects what it previously rejected
map2

Map over two inputs
map_if

Apply a function to each element of a vector conditionally
modify

Modify elements selectively
map_raw

Functions that return raw vectors
modify_tree

Recursively modify a list
modify_in

Modify a pluck location
map_depth

Map/modify elements at given depth
quietly

Wrap a function to capture side-effects
purrr-package

purrr: Functional Programming Tools
possibly

Wrap a function to return a value instead of an error
progress_bars

Progress bars in purrr
pluck

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

Compute the depth of a vector
pmap

Map over multiple input simultaneously (in "parallel")
%>%

Pipe operator
purrr_error_indexed

Indexed errors (purrr_error_indexed)
prepend

Prepend a vector
rate-helpers

Create delaying rate settings
safely

Wrap a function to capture errors
rerun

Re-run expressions multiple times
rate_sleep

Wait for a given time
slowly

Wrap a function to wait between executions
splice

Splice objects and lists of objects into a list
reduce

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

Objects exported from other packages
rbernoulli

Generate random sample from a Bernoulli distribution
rdunif

Generate random sample from a discrete uniform distribution
when

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

Update a list with formulas
transpose

Transpose a list.
array-coercion

Coerce array to list
as_mapper

Convert an object into a mapper function
as_vector

Coerce a list to a vector
attr_getter

Create an attribute getter function
compose

Compose multiple functions together to create a new function
auto_browse

Wrap a function so it will automatically browse() on error
chuck

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

Accumulate intermediate results of a vector reduction
cross

Produce all combinations of list elements
along

Create a list of given length