purrr v0.2.3


Monthly downloads



Functional Programming Tools

A complete and consistent functional programming toolkit for R.



CRAN\_Status\_Badge Build Status


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.


# The easiest way to get purrr is to install the whole tidyverse:

# Alternatively, install just purrr:

# Or the the development version from GitHub:
# install.packages("devtools")


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.


mtcars %>%
  split(.$cyl) %>% # from base R
  map(~ lm(mpg ~ wt, data = .)) %>%
  map(summary) %>%
#>         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 errror.

  • All map() functions either accept function, formulas (used for succinctly generating anonymous functions), a character vector (used to extract components by name), or a numeric vector (used to extract by position).

Functions in purrr

Name Description
accumulate Accumulate recursive folds across a list
along Helper to create vectors with matching length.
array-coercion Coerce array to list
as_mapper Convert an object into a mapper function
every Do every or some elements of a list satisfy a predicate?
flatten Flatten a list of lists into a simple vector.
as_vector Coerce a list to a vector
compose Compose multiple functions
cross Produce all combinations of list elements
detect Find the value or position of the first match.
invoke Invoke functions.
is_numeric Test is an object is integer or double
map Apply a function to each element of a vector
map2 Map over multiple inputs simultaneously.
head_while Find head/tail that all satisfies a predicate.
imap Apply a function to each element of a vector, and its index
%>% Pipe operator
get-attr Infix attribute accessor
has_element Does a list contain an object?
modify Modify elements selectively
keep Keep or discard elements using a predicate function.
lift Lift the domain of a function
prepend Prepend a vector
purrr-package purrr: Functional Programming Tools
list_modify Modify a list
lmap Apply a function to list-elements of a list
null-default Default value for NULL.
partial Partial apply a function, filling in some arguments.
set_names Set names in a vector
splice Splice objects and lists of objects into a list
negate Negate a predicate function.
transpose Transpose a list.
vec_depth Compute the depth of a vector
when Match/validate a set of conditions for an object and continue with the action associated with the first valid match.
rbernoulli Generate random sample from a Bernoulli distribution
rdunif Generate random sample from a discrete uniform distribution
pluck Pluck out a single an element from a vector or environment
rerun Re-run expressions multiple times.
safely Capture side effects.
reduce Reduce a list to a single value by iteratively applying a binary function.
reexports Objects exported from other packages
No Results!

Vignettes of purrr

No Results!

Last month downloads


URL http://purrr.tidyverse.org, https://github.com/tidyverse/purrr
BugReports https://github.com/tidyverse/purrr/issues
License GPL-3 | file LICENSE
LazyData true
RoxygenNote 6.0.1
VignetteBuilder knitr
NeedsCompilation yes
Packaged 2017-08-02 09:31:27 UTC; lionel
Repository CRAN
Date/Publication 2017-08-02 18:31:42 UTC

Include our badge in your README