map
-like functions, similar to the ones implemented in purrr:
map()
returns the results of.f
applied to.x
as list. If.f
is not a function,map
will call[[
on all elements of.x
using the value of.f
as index.imap()
applies.f
to each value of.x
(passed as first argument) and its name (passed as second argument). If.x
does not have names, a sequence along.x
is passed as second argument instead.pmap()
expects.x
to be a list of vectors of equal length, and then applies.f
to the first element of each vector of.x
, then the second element of.x
, and so on.map_if()
applies.f
to each element of.x
where the predicate.p
evaluates toTRUE
.map_at()
applies.f
to each element of.x
referenced by.at
. All other elements remain unchanged.keep()
keeps those elements of.x
where predicate.p
evaluates toTRUE
.discard()
discards those elements of.x
where predicate.p
evaluates toTRUE
.every()
isTRUE
if predicate.p
evaluates toTRUE
for each.x
.some()
isTRUE
if predicate.p
evaluates toTRUE
for at least one.x
.detect()
returns the first element where predicate.p
evaluates toTRUE
.walk()
,iwalk()
andpwalk()
are the counterparts tomap()
,imap()
andpmap()
, but just visit (or change by reference) the elements of.x
. They return input.x
invisibly.
Additionally, the functions map()
, imap()
and pmap()
have type-safe variants with the following suffixes:
*_lgl()
returns alogical(length(.x))
.*_int()
returns ainteger(length(.x))
.*_dbl()
returns adouble(length(.x))
.*_chr()
returns acharacter(length(.x))
.*_br()
returns an object where the results of.f
are put together withbase::rbind()
.*_bc()
returns an object where the results of.f
are put together withbase::cbind()
.*_dtr()
returns adata.table::data.table()
where the results of.f
are put together in anbase::rbind()
fashion.*_dtc()
returns adata.table::data.table()
where the results of.f
are put together in anbase::cbind()
fashion.
Usage
map(.x, .f, ...)
map_lgl(.x, .f, ...)
map_int(.x, .f, ...)
map_dbl(.x, .f, ...)
map_chr(.x, .f, ...)
map_br(.x, .f, ...)
map_bc(.x, .f, ...)
map_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL)
map_dtc(.x, .f, ...)
pmap(.x, .f, ...)
pmap_lgl(.x, .f, ...)
pmap_int(.x, .f, ...)
pmap_dbl(.x, .f, ...)
pmap_chr(.x, .f, ...)
pmap_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL)
pmap_dtc(.x, .f, ...)
imap(.x, .f, ...)
imap_lgl(.x, .f, ...)
imap_int(.x, .f, ...)
imap_dbl(.x, .f, ...)
imap_chr(.x, .f, ...)
imap_dtr(.x, .f, ..., .fill = FALSE, .idcol = NULL)
imap_dtc(.x, .f, ...)
keep(.x, .f, ...)
discard(.x, .p, ...)
map_if(.x, .p, .f, ...)
# Default S3 method
map_if(.x, .p, .f, ...)
map_at(.x, .at, .f, ...)
every(.x, .p, ...)
some(.x, .p, ...)
detect(.x, .p, ...)
walk(.x, .f, ...)
iwalk(.x, .f, ...)
pwalk(.x, .f, ...)
Arguments
- .x
- .f
(
function()
|character()
|integer()
)
Function to apply, or element to extract by name (if.f
ischaracter()
) or position (if.f
isinteger()
).- ...
(
any
)
Additional arguments passed down to.f
or.p
.- .fill
(
logical(1)
)
Passed down todata.table::rbindlist()
.- .idcol
(
logical(1)
)
Passed down todata.table::rbindlist()
.- .p
(
function()
|logical()
)
Predicate function.- .at
(
character()
|integer()
|logical()
)
Index vector.