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 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.
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.
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).
Please note that the purrr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.