map_if | R Documentation |
The functions map_if()
and map_at()
take .x
as input, apply
the function .f
to some of the elements of .x
, and return a
list of the same length as the input.
map_if()
takes a predicate function .p
as input to determine
which elements of .x
are transformed with .f
.
map_at()
takes a vector of names or positions .at
to specify
which elements of .x
are transformed with .f
.
map_if(.x, .p, .f, ..., .else = NULL)
map_at(.x, .at, .f, ..., .progress = FALSE)
Other map variants:
imap()
,
lmap()
,
map2()
,
map_depth()
,
map()
,
modify()
,
pmap()
# Use a predicate function to decide whether to map a function:
iris |> map_if(is.factor, as.character) |> str()
# Specify an alternative with the `.else` argument:
iris |> map_if(is.factor, as.character, .else = as.integer) |> str()
# Use numeric vector of positions select elements to change:
iris |> map_at(c(4, 5), is.numeric) |> str()
# Use vector of names to specify which elements to change:
iris |> map_at("Species", toupper) |> str()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.