You are on page 1of 2

Apply functions with purrr : : CHEATSHEET

Map Functions
ONE LIST TWO LISTS MANY LISTS
map(.x, .f, …) Apply a function to each element map2(.x, .y, .f, …) Apply a function to pairs of pmap(.l, .f, …) Apply a function to groups of
of a list or vector, and return a list. elements from two lists or vectors, return a list. elements from a list of lists or vectors, return a list.
x <- list(a = 1:10, b = 11:20, c = 21:30) y <- list(1, 2, 3); z <- list(4, 5, 6); l2 <- list(x = "a", y = "z") pmap(
l1 <- list(x = c("a", "b"), y = c("c", "d")) map2(x, y,\(x, y) x*y) list(x, y, z),
map(l1, sort, decreasing = TRUE) function(first, second, third) first * (second + third)
)
fun( ,…) fun( , ,…) fun( , , ,…)
map( , fun, …) fun( ,…) map2( , ,fun,…) fun( , ,…) pmap( ,fun,…) fun( , , ,…)
fun( ,…) fun( , ,…) fun( , , ,…)

1.0 map_dbl(.x, .f, …) 1.0 map2_dbl(.x, .y, .f, …) Return 1.0 pmap_dbl(.l, .f, …)
2.5 Return a double vector. 2.5 a double vector. 2.5 Return a double vector.
3.0 map_dbl(x, mean) 3.0 map2_dbl(y, z, ~ .x / .y) 3.0 pmap_dbl(list(y, z), ~ .x / .y)

map_int(.x, .f, ...) map2_int(.x, .y, .f, …) Return 1


pmap_int(.l, .f, …)
1 1
Return an integer vector. an integer vector. Return an integer vector.
2
2 2
map2_int(y, z, `+`) pmap_int(list(y, z), `+`)
3 map_int(x, length) 3 3

map2_chr(.x, .y, .f, …) Return pmap_chr(.l, .f, …)


a
map_chr(.x, .f, …) a a character vector. a Return a character vector.
b
Return a character vector. b map2_chr(l1, l2, paste, b pmap_chr(list(l1, l2), paste,
c
map_chr(l1, paste, collapse = "") c collapse = ",", sep = ":") c collapse = ",", sep = ":")

map_lgl(.x, .f, …) map2_lgl(.x, .y, .f, …) Return T


pmap_lgl(.l, .f, …)
T 1.0
Return a logical vector. a logical vector. Return a logical vector.
F
T
map_lgl(x, is.integer) 2.5
map2_lgl(l2, l1, `%in%`) pmap_lgl(list(l2, l1), `%in%`)
F 3.0 T

map_vec(.x, .f, ...) map2_vec(.x, .f, ...) pmap_vec(.l, .f, …)


a Return a vector that is of the a Return a vector that is of the a Return a vector that is of the
b simplest common type. b simplest common type. b simplest common type.
c map_vec(l1, paste, collapse = “") c map2_vec(l1, l2, paste, c pmap_vec(list(l1, l2), paste,
collapse = ",", sep = ":") collapse = ",", sep = ":")

walk(.x, .f, ...) Trigger side walk2(.x, .y, .f, ...) Trigger pwalk(.l, .f, ...) Trigger side
e ects, return invisibly. side e ects, return invisibly. e ects, return invisibly.
walk(x, print) walk2(objs, paths, save) pwalk(list(objs, paths), save)

imap(.x, .f, ...) is shorthand for map2(.x,


names(.x), .f) or map2(.x, seq_along(.x), .f)
Function Shortcuts depending on whether .x is named or not.

Use \(x) with functions like map() that have Use \(x, y) with functions like map2() that have Use \(x, y, z) etc with functions like pmap() that Use \(x, y) with functions like imap(). .x will get
single arguments. two arguments. have many arguments. the list value and .y will get the index, or name if
map(l, \(x) x + 2) map2(l, p, \(x, y) x + y) pmap(list(x, y, z), \(x, y, z) x + y / z) available.
becomes becomes becomes imap(list("a", "b", "c"), \(x, y) paste0(y, ": ", x))
map(l, function(x) x + 2) map2(l, p, function(l, p) l + p) pmap(list(x, y, z), function(x, y, z) x * (y + z)) outputs "index: value" for each item

Use a string or an integer with any map function to index list elements by name or position. map(l, "name") becomes map(l, function(x) x[["name"]])
CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at purrr.tidyverse.org • HTML cheatsheets at pos.it/cheatsheets •. purrr 1.0.1 • Updated: 2023-07
ff
ff
ff
ft
Vectors
Modify a NULL b compact(.x, .p = identity)
Pluck Concatenate
b Discard empty elements. a b pluck(.x, ..., .default=NULL)
modify(.x, .f, ...) Apply a
c NULL compact(x) b Select an element by name or x1 <- list(a = 1, b = 2, c = 3)
a a
b b function to each element. Also
c
index. Also attr_getter() and x2 <- list(
keep_at(x, at) d a = data.frame(x = 1:2),
c c modify2(), and imodify(). a a chuck().
d d modify(x, ~.+ 2) b Keep/discard elements based pluck(x, "b") b = data.frame(y = "a")
c by name or position. x |> pluck(“b") )
Conversely, discard_at().
modify_at(.x, .at, .f, ...) Apply a keep_at(x, “a”)
a a list_c(x) Combines elements
b b function to selected elements. a a assign_in(x, where, value) into a vector by concatenating
c c Also map_at(). a p set_names(x, nm = x) b b Assign a value to a location them together.
d d modify_at(x, "b", ~.+ 2) b q Set the names of a vector/list c c
using pluck selection. list_c(x1)
d d
c r directly or with a function. assign_in(x, "b", 5)
set_names(x, c("p", "q", "r")) x |> assign_in("b", 5)
modify_if(.x, .p, .f, ...) Apply a set_names(x, tolower)
a a list_rbind(x) Combines
b b function to elements that pass elements into a data frame by
c
d
c
d
a test. Also map_if().
modify_if(x, is.numeric,~.+2)
Predicate functions a
b
a
fun( )
modify_in(.x, .where, .f) Apply
a function to a value at a
row-binding them together.
list_rbind(x2)
a keep(.x, .p, …) c c selected location.
b
b Keep elements that pass a d d modify_in(x, "b", abs)
xy xy modify_depth(.x, .depth, .f, ...) c logical test. x |> modify_in("b", abs)
list_cbind(x) Combines
a a Apply function to each element Conversely, discard(). elements into a data frame by
b b at a given level of a list. Also keep(x, is.numeric) column-binding them
c c
map_depth(). together.
modify_depth(x, 1, ~.+ 2) a a head_while(.x, .p, …) list_cbind(x2)
b b Return head elements until
c one does not pass.
d Also tail_while().
head_while(x, is.character) Reshape
Reduce xy
detect(.x, .f, ..., dir = a list_flatten(.x) Remove a level x y
list_ranspose(.l, .names =
a c b a a
c("forward", "backward"), of indexes from a list. b b NULL)
b c
reduce(.x, .f, ..., .init, . c .right = NULL, .default = NULL) list_flatten(x) c c Transposes the index order in
dir = c("forward", "backward")) Find first element to pass. a multi-level list.
Apply function recursively to each element of a detect(x, is.character) list_transpose(x)
list or vector. Also reduce2().
reduce(x, sum) detect_index(.x, .f, ..., dir =
a 3
c("forward", "backward"),
func + a b c d
a b
func( , )
b
c .right = NULL) Find index of List-Columns map(), map2(), or pmap() return lists and will
c first element to pass. create new list-columns.
func( , ) detect_index(x, is.character)
d List-columns are columns of a list function,
func( , ) max seq data frame where each element is return list list-columns
every(.x, .p, …) 3 <int [3]> a list or vector instead of an atomic starwars |>
a FALSE
b Do all elements pass a test? 4 <int [4]> value. Columns can also be lists of transmute(ships = map2(vehicles,
c every(x, is.character) 5 <int [5]> data frames. See tidyr for more starships,
accumulate(.x, .f, ..., .init) Reduce a list, but also about nested data and list
return intermediate results. Also accumulate2().
column function append)
some(.x, .p, …) columns.
accumulate(x, sum) a TRUE
Do some elements pass a test?
b
c some(x, is.character) Su ixed map functions like map_int() return an
a
a b WORK WITH LIST-COLUMNS atomic data type and will simplify list-columns
func + a b c d func( , ) none(.x, .p, …) into regular columns.
c a TRUE Manipulate list-columns like any other kind of
func( , ) b Do no elements pass a test? column, using dplyr functions like mutate().
d none(x, is.character) list function,
func( , ) c Because each element is a list, use map return int
functions within a column function to starwars |>
a TRUE has_element(.x, .y) manipulate each element. mutate(n_films = map_int(films, length))
b Does a list contain an element?
c has_element(x, "foo") column function list-column

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at purrr.tidyverse.org • HTML cheatsheets at pos.it/cheatsheets •. purrr 1.0.1 • Updated: 2023-07
ff
ft

You might also like