Learn R Programming

⚠️There's a newer version (1.0.4) 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

1,212,992

Version

0.3.5

License

GPL-3 | file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Lionel Henry

Last Published

October 6th, 2022

Functions in purrr (0.3.5)

exec

Execute a function
faq-adverbs-export

Best practices for exporting adverb-wrapped functions
cross

Produce all combinations of list elements
as_mapper

Convert an object into a mapper function
along

Create a list of given length
done

Done box
every

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

Does a list contain an object?
imap

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

Find head/tail that all satisfies a predicate.
insistently

Transform a function to make it run insistently or slowly
flatten

Flatten a list of lists into a simple vector.
get-attr

Infix attribute accessor
map_if

Apply a function to each element of a vector conditionally
map

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

Map over multiple inputs simultaneously.
modify

Modify elements selectively
keep

Keep or discard elements using a predicate function.
lmap

Apply a function to list-elements of a list
lift

Lift the domain of a function
invoke

Invoke functions.
list_modify

Modify a list
null-default

Default value for NULL
rate-helpers

Create delaying rate settings
partial

Partial apply a function, filling in some arguments.
is_numeric

Test is an object is integer or double
prepend

Prepend a vector
rate_sleep

Wait for a given time
purrr-package

purrr: Functional Programming Tools
pluck

Pluck or chuck a single element from a vector or environment
set_names

Set names in a vector
reexports

Objects exported from other packages
%>%

Pipe operator
modify_in

Modify a pluck location
negate

Negate a predicate function.
reduce_right

Reduce from the right (retired)
reduce

Reduce a list to a single value by iteratively applying a binary function
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.
zap

Zap an element
splice

Splice objects and lists of objects into a list
safely

Capture side effects.
rerun

Re-run expressions multiple times.
vec_depth

Compute the depth of a vector
transpose

Transpose a list.
rep_along

Repeat a value with matching length
compose

Compose multiple functions
accumulate

Accumulate intermediate results of a vector reduction
at_depth

Map at depth
array-coercion

Coerce array to list
detect

Find the value or position of the first match
as_vector

Coerce a list to a vector
attr_getter

Create an attribute getter function