Professional Documents
Culture Documents
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, 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(1:10, 11:20, 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 • info@posit.co • posit.co • Learn more at purrr.tidyverse.org • purrr 0.3.4 • Updated: 2021-07
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.na) 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. WORK WITH LIST-COLUMNS
a a head_while(.x, .p, …) d d assign_in(x, "b", 5) a a modify_if(.x, .p, .f, ...) Apply a Manipulate list-columns like any other kind of
b b Return head elements until x %>% assign_in("b", 5) b b function to elements that pass column, using dplyr functions like mutate() and
c one does not pass. c c a test. Also map_if(). transmute(). Because each element is a list, use
d Also tail_while(). d d modify_if(x, is.numeric,~.+2) map functions within a column function to
head_while(x, is.character) manipulate each element.
a a modify_in(.x, .where, .f)
b fun( ) Apply a function to a value at xy xy modify_depth(.x, .depth, .f, ...)
detect(.x, .f, ..., dir = c c a selected location. a a Apply function to each element
a c
b c("forward", "backward"), d d modify_in(x, "b", abs) b b
at a given level of a list. Also map(), map2(), or pmap() return lists and will
c c create new list-columns.
c .right = NULL, .default = NULL) x %>% modify_in("b", abs) map_depth().
Find first element to pass. modify_depth(x, 2, ~.+ 2) list function,
detect(x, is.character) return list list-columns
starwars %>%
a 3 detect_index(.x, .f, ..., dir = Reshape Combine transmute(ships = map2(vehicles,
starships,
b c("forward", "backward"), column function append)
c .right = NULL) Find index of a flatten(.x) Remove a level of + append(x, values, a er =
first element to pass. b indexes from a list. length(x)) Add values to end of
detect_index(x, is.character) c Also flatten_chr() etc. list.
flatten(x) append(x, list(d = 1))
Su ixed map functions like map_int() return an
a FALSE every(.x, .p, …) atomic data type and will simplify list-columns
b Do all elements pass a test? array_tree(array, margin = + prepend(x, values, before = 1) into regular columns.
c every(x, is.character) NULL) Turn array into list. Add values to start of list.
Also array_branch(). prepend(x, list(d = 1)) list function,
return int
array_tree(x, margin = 3)
a TRUE some(.x, .p, …) starwars %>%
b Do some elements pass a test? splice(…) Combine objects into mutate(n_films = map_int(films, length))
+
c some(x, is.character) + cross2(.x, .y, .filter = NULL) a list, storing S3 objects as sub-
All combinations of .x and .y. + lists. column function list-column
Also cross(), cross3(), and splice(x, y, "foo")
a TRUE none(.x, .p, …) cross_df().
b Do no elements pass a test? cross2(1:3, 4:6)
none(x, is.character)
Reduce
c
CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at purrr.tidyverse.org • purrr 0.3.4 • Updated: 2021-07
ff
ft
ft