Map family

The map(.x, .f) functions transforms each element of the vector .x with the function .f, returning a vector defined by the suffix (_lgl, _chr() etc). walk() is a variant for functions called primarily for their side-effects; it returns .x invisibly.

As well as functions, .f, can take numbers and characters (used as a shorthand for [[), and formulas (used as a succint function definition). See details in as_mapper()

map map_lgl map_chr map_int map_dbl map_dfr map_dfc walk

Apply a function to each element of a vector

as_mapper get_attr

Convert an object into a mapper function

Map variants

A rich set of variants builds on the basic idea of map(): modify() modifies “in place”, returning a vector the same type as .x; map2() iterates over two vectors in parallel; pmap() (parallel map) iterates over a list of vectors; imap() (index map) is a shortcut for the common pattern map2(x, names(x)); invoke() calls each function in a list.

map2 map2_lgl map2_int map2_dbl map2_chr map2_dfr map2_dfc walk2 pmap pmap_lgl pmap_int pmap_dbl pmap_chr pmap_dfr pmap_dfc pwalk

Map over multiple inputs simultaneously.

modify

Modify elements "in-place"

imap imap_lgl imap_chr imap_int imap_dbl imap_dfr imap_dfc iwalk

Apply a function to each element of a vector, and its index

lmap lmap_if lmap_at

Apply a function to list-elements of a list

invoke invoke_map invoke_map_lgl invoke_map_int invoke_map_dbl invoke_map_chr invoke_map_dfr invoke_map_dfc

Invoke functions.

Predicate functionals

A predicate function is a function that either returns TRUE or FALSE. The predicate functionals take a vector and a predicate function and do something useful.

detect detect_index

Find the value or position of the first match.

every some

Do every or some elements of a list satisfy a predicate?

has_element

Does a list contain an object?

head_while tail_while

Find head/tail that all satisfies a predicate.

keep discard compact

Keep or discard elements using a predicate function.

negate

Negate a predicate function.

prepend

Prepend a vector

Other vector transforms

A grab bag of useful tools for manipulating vectors.

accumulate accumulate_right

Accumulate recursive folds across a list

cross cross2 cross3 cross_df

Produce all combinations of list elements

flatten flatten_lgl flatten_int flatten_dbl flatten_chr flatten_dfr flatten_dfc

Flatten a list of lists into a simple vector.

list_modify list_update

Modify a list

reduce reduce_right reduce2 reduce2_right

Reduce a list to a single value by iteratively applying a binary function.

splice

Splice objects and lists of objects into a list

transpose

Transpose a list.

Adverbs

Adverbs modify the action of a function; taking a function as input and returning a function with modified action as output.

compose

Compose multiple functions

lift lift_dl lift_dv lift_vl lift_vd lift_ld lift_lv

Lift the domain of a function

negate

Negate a predicate function.

partial

Partial apply a function, filling in some arguments.

safely quietly possibly auto_browse

Capture side effects.

Misc

%||%

Default value for NULL.

array_branch array_tree

Coerce array to list

as_vector simplify simplify_all

Coerce a list to a vector

depth

Compute the depth of a vector

rbernoulli

Generate random samples from a Bernoulli distribution

rdunif

Generate random samples from a discrete uniform distribution

rerun

Re-run expressions multiple times.

set_names

Set names in a vector