These functions work exactly the same as typed variants of purrr::map(), purrr::map2(), purrr::pmap(), purrr::imap() and xmap() (e.g. purrr::map_chr()), but automatically determine the type.

map_vec(.x, .f, ...)

map2_vec(.x, .y, .f, ...)

pmap_vec(.l, .f, ...)

imap_vec(.x, .f, ...)

xmap_vec(.l, .f, ...)

Arguments

.x

A list or atomic vector.

.f

A function, formula, or vector (not necessarily atomic).

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

This syntax allows you to create very compact anonymous functions.

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to .f

.y

A vector the same length as .x. Vectors of length 1 will be recycled.

.l

A list of vectors, such as a data frame. The length of .l determines the number of arguments that .f will be called with. List names will be used if present.

Value

Equivalent to the typed variants of purrr::map(), purrr::map2(), purrr::pmap(), purrr::imap() and xmap() with the type automatically determined.

If the output contains multiple types, the type is determined from the highest type of the components in the hierarchy raw < logical < integer < double < complex < character < list (as in c()).

If the output contains elements that cannot be coerced to vectors (e.g. lists), the output will be a list.

See also

Examples

fruits <- c("apple", "banana", "cantaloupe", "durian", "eggplant") desserts <- c("bread", "cake", "cupcake", "muffin", "streudel") x <- sample(5) y <- sample(5) z <- sample(5) names(z) <- fruits map_vec(x, ~ . ^ 2)
#> [1] 9 1 16 4 25
map_vec(fruits, paste0, "s")
#> [1] "apples" "bananas" "cantaloupes" "durians" "eggplants"
map2_vec(x, y, ~ .x + .y)
#> [1] 5 6 7 3 9
map2_vec(fruits, desserts, paste)
#> [1] "apple bread" "banana cake" "cantaloupe cupcake" #> [4] "durian muffin" "eggplant streudel"
pmap_vec(list(x, y, z), sum)
#> [1] 7 9 8 8 13
pmap_vec(list(x, fruits, desserts), paste)
#> [1] "3 apple bread" "1 banana cake" "4 cantaloupe cupcake" #> [4] "2 durian muffin" "5 eggplant streudel"
imap_vec(x, ~ .x + .y)
#> [1] 4 3 7 6 10
imap_vec(x, ~ paste0(.y, ": ", .x))
#> [1] "1: 3" "2: 1" "3: 4" "4: 2" "5: 5"
imap_vec(z, paste)
#> apple banana cantaloupe durian eggplant #> "2 apple" "3 banana" "1 cantaloupe" "5 durian" "4 eggplant"
xmap_vec(list(x, y), ~ .x * .y)
#> [1] 6 2 8 4 10 15 5 20 10 25 9 3 12 6 15 3 1 4 2 5 12 4 16 8 20
xmap_vec(list(fruits, desserts), paste)
#> [1] "apple bread" "banana bread" "cantaloupe bread" #> [4] "durian bread" "eggplant bread" "apple cake" #> [7] "banana cake" "cantaloupe cake" "durian cake" #> [10] "eggplant cake" "apple cupcake" "banana cupcake" #> [13] "cantaloupe cupcake" "durian cupcake" "eggplant cupcake" #> [16] "apple muffin" "banana muffin" "cantaloupe muffin" #> [19] "durian muffin" "eggplant muffin" "apple streudel" #> [22] "banana streudel" "cantaloupe streudel" "durian streudel" #> [25] "eggplant streudel"