conditional-map

0th

Percentile

Modify elements conditionally

map_if() maps a function over the elements of .x satisfying a predicate. map_at() is similar but will modify the elements corresponding to a character vector of names or a mumeric vector of positions.

Usage
map_if(.x, .p, .f, ...)

map_at(.x, .at, .f, ...)

Arguments
.x

A list or atomic vector.

.p

A single predicate function, a formula describing such a predicate function, or a logical vector of the same length as .x. Alternatively, if the elements of .x are themselves lists of objects, a string indicating the name of a logical element in the inner lists. Only those elements where .p evaluates to TRUE will be modified.

.f

A function, formula, or atomic vector.

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function with two arguments, .x or . and .y. This allows you to create very compact anonymous functions with up to two inputs.

If character or integer vector, e.g. "y", it is converted to an extractor function, function(x) x[["y"]]. To index deeply into a nested list, use multiple values; c("x", "y") is equivalent to z[["x"]][["y"]]. You can also set .null to set a default to use instead of NULL for absent components.

...

Additional arguments passed on to .f.

.at

A character vector of names or a numeric vector of positions. Only those elements corresponding to .at will be modified.

Value

A list.

Aliases
  • conditional-map
  • map_if
  • map_at
Examples
# NOT RUN {
# Convert factors to characters
iris %>%
  map_if(is.factor, as.character) %>%
  str()

# Specify which columns to map with a numeric vector of positions:
mtcars %>% map_at(c(1, 4, 5), as.character) %>% str()

# Or with a vector of names:
mtcars %>% map_at(c("cyl", "am"), as.character) %>% str()

list(x = rbernoulli(100), y = 1:100) %>%
  transpose() %>%
  map_if("x", ~ update_list(., y = ~ y * 100)) %>%
  transpose() %>%
  simplify_all()

# }
Documentation reproduced from package purrr, version 0.2.2.2, License: GPL-3 | file LICENSE

Community examples

Looks like there are no examples yet.