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, ..., .class = NULL)
map2_vec(.x, .y, .f, ..., .class = NULL)
pmap_vec(.l, .f, ..., .class = NULL)
imap_vec(.x, .f, ..., .class = NULL)
xmap_vec(.l, .f, ..., .class = NULL)
A list or atomic vector.
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
If .class
is specified, all
A vector the same length as .x
.
Vectors of length 1 will be recycled.
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.
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.
The original functions: purrr::map()
, purrr::map2()
,
purrr::pmap()
, purrr::imap()
and xmap()
Parallelized equivalents: future_map_vec()
, future_map2_vec()
,
future_pmap_vec()
, future_imap_vec()
and future_xmap_vec()
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] 4 16 25 1 9
map_vec(fruits, paste0, "s")
#> [1] "apples" "bananas" "cantaloupes" "durians" "eggplants"
map2_vec(x, y, ~ .x + .y)
#> [1] 4 7 6 5 8
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] 6 8 11 8 12
pmap_vec(list(x, fruits, desserts), paste)
#> [1] "2 apple bread" "4 banana cake" "5 cantaloupe cupcake"
#> [4] "1 durian muffin" "3 eggplant streudel"
imap_vec(x, ~ .x + .y)
#> [1] 3 6 8 5 8
imap_vec(x, ~ paste0(.y, ": ", .x))
#> [1] "1: 2" "2: 4" "3: 5" "4: 1" "5: 3"
imap_vec(z, paste)
#> apple banana cantaloupe durian eggplant
#> "2 apple" "1 banana" "5 cantaloupe" "3 durian" "4 eggplant"
xmap_vec(list(x, y), ~ .x * .y)
#> [1] 4 8 10 2 6 6 12 15 3 9 2 4 5 1 3 8 16 20 4 12 10 20 25 5 15
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"