Purrr
Purrr
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)
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.
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 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