purrr v0.2.4

0

0th

Percentile

Functional Programming Tools

A complete and consistent functional programming toolkit for R.

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")


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