You are on page 1of 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, 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.0 map2_int(.x, .y, .f, …) 1 pmap_int(.l, .f, …) 1 imap_int(.x, .f, ...)
2 Return an integer vector. 2.5 Return an integer vector. 2 Return an integer vector. 2 Return an integer vector.
3 map_int(x, length) 3.0 map2_int(y, z, `+`) 3 pmap_int(list(y, z), `+`) 3 imap_int(y, ~ .y)

a map_chr(.x, .f, …) 1.0 map2_chr(.x, .y, .f, …) a pmap_chr(.l, .f, …) a imap_chr(.x, .f, …)
b Return a character vector. 2.5 Return a character vector. b Return a character vector. b Return a character vector.
c map_chr(l1, paste, collapse = "") 3.0 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.
map(l, ~ . + 2) map2(l, p, ~ .x +.y) pmap(list(a, b, c), ~ ..3 + ..1 - ..2) imap(list(a, b, c), ~ paste0(.y, ": ", .x)
becomes becomes becomes outputs "index: value" for each item
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)

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"]])
RStudio® is a trademark of RStudio, PBC • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at purrr.tidyverse.org • purrr 0.3.4 • Updated: 2021-07
ff
ff
ff
ff































































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

xy x y transpose(.l, .names = NULL)


a TRUE has_element(.x, .y) a a Transposes the index order in reduce(.x, .f, ..., .init, .dir = c("forward", accumulate(.x, .f, ..., .init) Reduce a list, but also
b Does a list contain an element? b b a multi-level list. "backward")) Apply function recursively to each return intermediate results. Also accumulate2().
c has_element(x, "foo") c c element of a list or vector. Also reduce2(). accumulate(x, sum)
transpose(x)
reduce(x, sum)
vec_depth(x) a
xy z 2 a p set_names(x, nm = x) a b a b
a Return depth (number of levels a b c d func( , ) a b c d
b
b q Set the names of a vector/list func + func + func( , )
of indexes). c r directly or with a function. c c
c func( , ) func( , )
vec_depth(x) set_names(x, c("p", "q", "r")) d
d
set_names(x, tolower) func( , ) func( , )

RStudio® is a trademark of RStudio, PBC • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at purrr.tidyverse.org • purrr 0.3.4 • Updated: 2021-07
ff




















ft































You might also like