0% found this document useful (0 votes)
56 views

Purrr

1. The document describes different map functions in purrr that apply a function to lists and vectors. 2. map() applies a function to each element of a list or vector and returns a list. map2() applies a function to pairs of elements from two lists or vectors and returns a list. 3. pmap() applies a function to groups of elements from a list of lists or vectors and returns a list, while imap() applies a function to each element and its index and returns a list.

Uploaded by

othon ishiyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Purrr

1. The document describes different map functions in purrr that apply a function to lists and vectors. 2. map() applies a function to each element of a list or vector and returns a list. map2() applies a function to pairs of elements from two lists or vectors and returns a list. 3. pmap() applies a function to groups of elements from a list of lists or vectors and returns a list, while imap() applies a function to each element and its index and returns a list.

Uploaded by

othon ishiyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Apply functions with purrr : : CHEAT SHEET

Map Functions
ONE LIST TWO LISTS MANY LISTS LISTS AND INDEXES
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 imap(.x, .f, ...) Apply .f to each element and its
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. index, 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(list(x, y, z), ~ ..1 * (..2 + ..3)) imap(y, ~ paste0(.y, ": ", .x))
l1 <- list(x = c("a", "b"), y = c("c", "d")) map2(x, y, ~ .x * .y)
map(l1, sort, decreasing = TRUE)
fun( ,…) fun( , ,…) fun( , , ,…) fun( , 1, …)
map( , fun, …) fun( ,…) map2( , ,fun,…) fun( , ,…) pmap( ,fun,…) fun( , , ,…) imap( , fun, …) fun( , 2, …)
fun( ,…) fun( , ,…) fun( , , ,…) fun( , 3, …)

1.0 map_dbl(.x, .f, …) 1.0 map2_dbl(.x, .y, .f, …) 1.0 pmap_dbl(.l, .f, …) 1.0 imap_dbl(.x, .f, …)
2.5 Return a double vector. 2.5 Return a double vector. 2.5 Return 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) 3.0 imap_dbl(y, ~ .y)

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

a map_chr(.x, .f, …) a map2_chr(.x, .y, .f, …) a pmap_chr(.l, .f, …) a imap_chr(.x, .f, …)


b Return a character vector. b Return a character vector. b Return a character vector. b Return a character vector.
c map_chr(l1, paste, collapse = "") c map2_chr(l1, l2, paste, c pmap_chr(list(l1, l2), paste, c imap_chr(y, ~ paste0(.y, ": ", .x))
collapse = ",", sep = ":") collapse = ",", sep = ":")
T map_lgl(.x, .f, …) T imap_lgl(.x, .f, …)
T Return a logical vector. 1.0 map2_lgl(.x, .y, .f, …) T pmap_lgl(.l, .f, …) T Return a logical vector.
F map_lgl(x, is.integer) 2.5 Return a logical vector. F Return a logical vector. F imap_lgl(l1, ~ is.character(.y))
3.0 map2_lgl(l2, l1, `%in%`) T pmap_lgl(list(l2, l1), `%in%`)
map_dfc(.x, .f, ...) imap_dfc(.x, .f, ...)
Return a data frame created map2_dfc(.x, .y, .f, ...) pmap_dfc(.l, .f, ...) Return Return a data frame created
by column-binding. Return a data frame created a data frame created by by column-binding.
map_dfc(l1, rep, 3) by column-binding. column-binding. imap_dfc(l2,
map2_dfc(l1, l2, pmap_dfc(list(l1, l2), ~ as.data.frame(c(.x, .y)))
map_dfr(.x, .f, ..., .id = NULL) ~ as.data.frame(c(.x, .y))) ~ as.data.frame(c(.x, .y)))
Return a data frame created imap_dfr(.x, .f, ..., .id = NULL)
by row-binding. map2_dfr(.x, .y, .f, ..., .id = pmap_dfr(.l, .f, ..., .id = Return a data frame created
map_dfr(x, summary) NULL) Return a data frame NULL) Return a data frame by row-binding.
created by row-binding. created by row-binding. imap_dfr(l2,
walk(.x, .f, ...) Trigger side map2_dfr(l1, l2, pmap_dfr(list(l1, l2), ~ as.data.frame(c(.x, .y)))
e ects, return invisibly. ~ as.data.frame(c(.x, .y))) ~ as.data.frame(c(.x, .y)))
walk(x, print) iwalk(.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. iwalk(z, ~ print(paste0(.y, ": ", .x)))
walk2(objs, paths, save) pwalk(list(objs, paths), save)
Function Shortcuts
Use ~ . with functions like map() that have single Use ~ .x .y with functions like map2() that have Use ~ ..1 ..2 ..3 etc with functions like pmap() Use ~ .x .y with functions like imap(). .x will get
arguments. two arguments. that have many arguments. the list value and .y will get the index, or name if
map(l, ~ . + 2) map2(l, p, ~ .x +.y) pmap(list(a, b, c), ~ ..3 + ..1 - ..2) available.
becomes becomes becomes imap(list(a, b, c), ~ paste0(.y, ": ", .x)
map(l, function(x) x + 2 )) map2(l, p, function(l, p) l + p) pmap(list(a, b, c), function(a, b, c) c + a -b) 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 • [email protected] • posit.co • Learn more at purrr.tidyverse.org • purrr 1.0.1 • Updated: 2023-05
ff
ff
ff
ff






ft



























































Work with Lists
Filter Index Modify List-
a
b
b keep(.x, .p, …)
Select elements that pass a
a
b
b pluck(.x, ..., .default=NULL)
Select an element by name or
a
b
a
b
modify(.x, .f, ...) Apply a
function to each element. Also
Columns
c logical test. c index. Also attr_getter() and c c modify2(), and imodify().
Conversely, discard(). d chuck(). d d modify(x, ~.+ 2) List-columns are columns of a
keep(x, is.numeric) pluck(x, "b") max seq data frame where each element is
x |> pluck("b") 3 <int [3]> a list or vector instead of an atomic
a a modify_at(.x, .at, .f, ...) Apply a 4 <int [4]> value. Columns can also be lists of
a NULL b compact(.x, .p = identity) b b function to selected elements. 5 <int [5]> data frames. See tidyr for more
b Drop empty elements. c c Also map_at(). about nested data and list
c NULL compact(x) a a assign_in(x, where, value) d d modify_at(x, "b", ~.+ 2) columns.
b b Assign a value to a location
c c using pluck selection.
a a head_while(.x, .p, …) d d assign_in(x, "b", 5) a a modify_if(.x, .p, .f, ...) Apply a WORK WITH LIST-COLUMNS
b b Return head elements until x |> assign_in("b", 5) b b function to elements that pass Manipulate list-columns like any other kind of
c one does not pass. c c a test. Also map_if(). column, using dplyr functions like mutate() and
d Also tail_while(). d d modify_if(x, is.numeric,~.+2) transmute(). Because each element is a list, use
head_while(x, is.character) map functions within a column function to
a a modify_in(.x, .where, .f)
b fun( ) Apply a function to a value at xy xy modify_depth(.x, .depth, .f, ...) manipulate each element.
detect(.x, .f, ..., dir = c c a selected location. a a Apply function to each element
a c b
d d b
b c("forward", "backward"), modify_in(x, "b", abs) c c at a given level of a list. Also
c .right = NULL, .default = NULL) x |> modify_in("b", abs) map_depth().
Find first element to pass. modify_depth(x, 1, ~.+ 2) map(), map2(), or pmap() return lists and will
detect(x, is.character) create new list-columns.

a 3 detect_index(.x, .f, ..., dir = Reshape Reduce starwars |>


list function,
return list list-columns

b c("forward", "backward"),
c .right = NULL) Find index of flatten(.x) Remove a level of reduce(.x, .f, ..., .init, .dir = c("forward", transmute(ships = map2(vehicles,
a
first element to pass. b indexes from a list. "backward")) Apply function recursively to each starships,
detect_index(x, is.character) c Also flatten_chr() etc. element of a list or vector. Also reduce2(). column function append)
flatten(x) reduce(x, sum)

a FALSE every(.x, .p, …) a b


b Do all elements pass a test? array_tree(array, margin = func + a b c d func( , ) Su ixed map functions like map_int() return an
c every(x, is.character) NULL) Turn array into list. c atomic data type and will simplify list-columns
Also array_branch(). func( , )
d
into regular columns.
z <- array(1:12, c(2,2,2)) func( , )
a TRUE some(.x, .p, …) array_tree(x, margin = 3) list function,
b Do some elements pass a test? return int
c some(x, is.character) starwars |>
xy x y transpose(.l, .names = NULL) mutate(n_films = map_int(films, length))
a a Transposes the index order in accumulate(.x, .f, ..., .init) Reduce a list, but also
a TRUE none(.x, .p, …) b b a multi-level list. column function list-column
Do no elements pass a test? c c return intermediate results. Also accumulate2().
b transpose(x) accumulate(x, sum)
c none(x, is.character)

a p set_names(x, nm = x) a
a TRUE has_element(.x, .y) b q Set the names of a vector/list a b c d a b
func( , )
b Does a list contain an element? func +
c r directly or with a function. c
c has_element(x, "foo") set_names(x, c("p", "q", "r")) func( , )
d
set_names(x, tolower) func( , )
xy z 2 pluck_depth(x)
a Return depth (number of levels
b of indexes).
c
pluck_depth(x)

CC BY SA Posit So ware, PBC • [email protected] • posit.co • Learn more at purrr.tidyverse.org • purrr 1.0.1 • Updated: 2023-05
ff







ft






































You might also like