imap {purrr} | R Documentation |
Apply a function to each element of a vector, and its index
Description
imap(x, ...)
, an indexed map, is short hand for
map2(x, names(x), ...)
if x
has names, or map2(x, seq_along(x), ...)
if it does not. This is useful if you need to compute on both the value
and the position of an element.
Usage
imap(.x, .f, ...)
imap_lgl(.x, .f, ...)
imap_chr(.x, .f, ...)
imap_int(.x, .f, ...)
imap_dbl(.x, .f, ...)
iwalk(.x, .f, ...)
Arguments
.x |
A list or atomic vector. |
.f |
A function, specified in one of the following ways:
|
... |
Additional arguments passed on to the mapped function. We now generally recommend against using # Instead of x |> map(f, 1, 2, collapse = ",") # do: x |> map(\(x) f(x, 1, 2, collapse = ",")) This makes it easier to understand which arguments belong to which function and will tend to yield better error messages. |
Value
A vector the same length as .x
.
See Also
Other map variants:
lmap()
,
map2()
,
map_depth()
,
map_if()
,
map()
,
modify()
,
pmap()
Examples
imap_chr(sample(10), paste)
imap_chr(sample(10), \(x, idx) paste0(idx, ": ", x))
iwalk(mtcars, \(x, idx) cat(idx, ": ", median(x), "\n", sep = ""))