Skip to content

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_raw() map_dfr() map_dfc() walk()
Apply a function to each element of a list or atomic vector
as_mapper()
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)).

map_if() map_at() map_depth()
Apply a function to each element of a vector conditionally
map2() map2_lgl() map2_int() map2_dbl() map2_chr() map2_raw() map2_dfr() map2_dfc() walk2() pmap() pmap_lgl() pmap_int() pmap_dbl() pmap_chr() pmap_raw() pmap_dfr() pmap_dfc() pwalk()
Map over multiple inputs simultaneously.
modify() modify_if() modify_at() modify2() imodify() modify_depth()
Modify elements selectively
imap() imap_lgl() imap_chr() imap_int() imap_dbl() imap_raw() 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

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() none()
Do every, some, or none of the 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

Plucking

Getting or setting a single element.

pluck() chuck() `pluck<-`()
Pluck or chuck a single element from a vector or environment
modify_in() assign_in()
Modify a pluck location
attr_getter()
Create an attribute getter function
`%@%`
Infix attribute accessor

Other vector transforms

A grab bag of useful tools for manipulating vectors.

accumulate() accumulate2()
Accumulate intermediate results of a vector reduction
cross() cross2() cross3() cross_df()
Produce all combinations of list elements
flatten() flatten_lgl() flatten_int() flatten_dbl() flatten_chr() flatten_raw() flatten_dfr() flatten_dfc()
Flatten a list of lists into a simple vector.
list_modify() list_merge()
Modify a list
reduce() reduce2()
Reduce a list to a single value by iteratively applying a binary function
rep_along
Repeat a value with matching length
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.

faq-adverbs-export
Best practices for exporting adverb-wrapped functions
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.
insistently() slowly()
Transform a function to make it run insistently or slowly
rate_sleep() rate_reset()
Wait for a given time
rate_delay() rate_backoff() is_rate()
Create delaying rate settings

Misc

null-default
Default value for NULL
array_branch() array_tree()
Coerce array to list
as_vector() simplify() simplify_all()
Coerce a list to a vector
rbernoulli()
Generate random sample from a Bernoulli distribution
rdunif()
Generate random sample from a discrete uniform distribution
exec
Execute a function
rerun()
Re-run expressions multiple times.
set_names
Set names in a vector
vec_depth()
Compute the depth of a vector
zap
Zap an element
done
Done box