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:

Try the purrr package in your browser

Any scripts or data that you put into this service are public.

purrr documentation built on Aug. 3, 2017, 1:03 a.m.