Learn R Programming

⚠️There's a newer version (1.0.2) of this package.Take me there.

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("devtools")
devtools::install_github("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(.$cyl) %>% # from base R
  map(~ lm(mpg ~ wt, data = .)) %>%
  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 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 this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copy Link

Version

Install

install.packages('purrr')

Monthly Downloads

877,436

Version

0.3.4

License

GPL-3 | file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

April 17th, 2020

Functions in purrr (0.3.4)

at_depth

Map at depth
faq-adverbs-export

Best practices for exporting adverb-wrapped functions
done

Done box
imap

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

Does a list contain an object?
head_while

Find head/tail that all satisfies a predicate.
get-attr

Infix attribute accessor
flatten

Flatten a list of lists into a simple vector.
exec

Execute a function
insistently

Transform a function to make it run insistently or slowly
every

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

Keep or discard elements using a predicate function.
map_if

Apply a function to each element of a vector conditionally
invoke

Invoke functions.
lmap

Apply a function to list-elements of a list
lift

Lift the domain of a function
map2

Map over multiple inputs simultaneously.
modify

Modify elements selectively
list_modify

Modify a list
is_numeric

Test is an object is integer or double
map

Apply a function to each element of a list or atomic vector
rate_sleep

Wait for a given time
partial

Partial apply a function, filling in some arguments.
%>%

Pipe operator
null-default

Default value for NULL
prepend

Prepend a vector
rate-helpers

Create delaying rate settings
negate

Negate a predicate function.
modify_in

Modify a pluck location
pluck

Pluck or chuck a single element from a vector or environment
purrr-package

purrr: Functional Programming Tools
reexports

Objects exported from other packages
rdunif

Generate random sample from a discrete uniform distribution
set_names

Set names in a vector
splice

Splice objects and lists of objects into a list
rbernoulli

Generate random sample from a Bernoulli distribution
rep_along

Repeat a value with matching length
vec_depth

Compute the depth of a vector
safely

Capture side effects.
transpose

Transpose a list.
rerun

Re-run expressions multiple times.
when

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

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

Reduce from the right (retired)
zap

Zap an element
as_vector

Coerce a list to a vector
as_mapper

Convert an object into a mapper function
attr_getter

Create an attribute getter function
detect

Find the value or position of the first match
array-coercion

Coerce array to list
compose

Compose multiple functions
cross

Produce all combinations of list elements
accumulate

Accumulate intermediate results of a vector reduction
along

Create a list of given length