Purrr Cheat Sheet

In R

Purrr is a popular R package that provides a set of tools for working with functions and vectors in a more efficient and intuitive way. It is designed to simplify common data manipulation tasks, such as filtering, mapping, and summarizing data, by providing a consistent and easy-to-use syntax.

One of the key features of Purrr is its ability to work with functions as first-class objects. This means that functions can be passed as arguments to other functions, returned as values, and stored in data structures. This makes it easy to create complex data transformations by chaining together simple functions.

Another important feature of Purrr is its support for vectorized operations. This means that many operations can be applied to entire vectors of data at once, rather than having to loop over each element individually. This can greatly improve the speed and efficiency of data manipulation tasks.

Functions

FunctionDescription
map()Apply a function to each element of a vector or list
map2()Apply a function to two vectors or lists element-wise
map_chr()Apply a function to each element of a vector or list and return a character vector
map_dbl()Apply a function to each element of a vector or list and return a double vector
map_int()Apply a function to each element of a vector or list and return an integer vector
map_lgl()Apply a function to each element of a vector or list and return a logical vector
map_if()Apply a function to each element of a vector or list if it meets a certain condition
map_at()Apply a function to specific elements of a vector or list
map_depth()Apply a function to each element of a nested list
pmap()Apply a function to multiple vectors or lists element-wise
pmap_chr()Apply a function to multiple vectors or lists element-wise and return a character vector
pmap_dbl()Apply a function to multiple vectors or lists element-wise and return a double vector
pmap_int()Apply a function to multiple vectors or lists element-wise and return an integer vector
pmap_lgl()Apply a function to multiple vectors or lists element-wise and return a logical vector
reduce()Reduce a vector or list to a single value using a function
accumulate()Accumulate a vector or list using a function

Arguments

ArgumentDescription
.fThe function to apply
.xThe vector or list to apply the function to
.yThe second vector or list to apply the function to
.keepWhether to keep the original structure of the input
.idThe name of the list element to use as an identifier
.namesThe names to use for the output list
.defaultThe default value to use if an element is missing
.f.argsAdditional arguments to pass to the function
.f.callWhether to include the function call in the output
.progressWhether to display a progress bar
.envThe environment to evaluate the function in

Examples

Map

library(purrr)

# Apply a function to each element of a vector
map(1:5, function(x) x^2)

# Apply a function to each element of a list
map(list(1:5, 6:10), sum)

# Apply a function to each element of a vector if it meets a certain condition
map_if(1:5, is.even, function(x) x^2)

# Apply a function to specific elements of a vector or list
map_at(list(1:5, 6:10), 1, function(x) x^2)

Reduce

# Reduce a vector to a single value
reduce(1:5, function(x, y) x + y)

# Reduce a list to a single value
reduce(list(1:5, 6:10), function(x, y) sum(x) + sum(y))

Pmap

# Apply a function to multiple vectors element-wise
pmap(list(1:5, 6:10), function(x, y) x + y)

# Apply a function to multiple lists element-wise
pmap(list(list(1, 2, 3), list(4, 5, 6)), sum)

Reference

https://purrr.tidyverse.org/

# #