The map functions transform their input by applying a function to each element and returning a vector the same length as the input.

  • map(), map_if() and map_at() always return a list. See the modify() family for versions that return an object of the same type as the input.

    The _if and _at variants take a predicate function .p that determines which elements of .x are transformed with .f.

  • map_lgl(), map_int(), map_dbl() and map_chr() each return an atomic vector of the indicated type (or die trying).

    The return value of .f must be of length one for each element of .x. If .f uses an extractor function shortcut, .default can be specified to handle values that are absent or empty. See as_mapper() for more on .default.

  • map_dfr() and map_dfc() return data frames created by row-binding and column-binding respectively. They require dplyr to be installed.

  • walk() calls .f for its side-effect and returns the input .x.

map(.x, .f, ...)

map_if(.x, .p, .f, ..., .else = NULL)

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

map_lgl(.x, .f, ...)

map_chr(.x, .f, ...)

map_int(.x, .f, ...)

map_dbl(.x, .f, ...)

map_raw(.x, .f, ...)

map_dfr(.x, .f, ..., .id = NULL)

map_dfc(.x, .f, ...)

walk(.x, .f, ...)

map_depth(.x, .depth, .f, ..., .ragged = FALSE)

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 the mapped function.

.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.

.else

A function applied to elements of .x for which .p returns FALSE.

.at

A character vector of names, positive numeric vector of positions to include, or a negative numeric vector of positions to exlude. Only those elements corresponding to .at will be modified.

.id

Either a string or NULL. If a string, the output will contain a variable with that name, storing either the name (if .x is named) or the index (if .x is unnamed) of the input. If NULL, the default, no variable will be created.

Only applies to _dfr variant.

.depth

Level of .x to map on. Use a negative value to count up from the lowest level of the list.

  • map_depth(x, 0, fun) is equivalent to fun(x).

  • map_depth(x, 1, fun) is equivalent to x <- map(x, fun)

  • map_depth(x, 2, fun) is equivalent to x <- map(x, ~ map(., fun))

.ragged

If TRUE, will apply to leaves, even if they're not at depth .depth. If FALSE, will throw an error if there are no elements at depth .depth.

Value

All functions return a vector the same length as .x.

map() returns a list, map_lgl() a logical vector, map_int() an integer vector, map_dbl() a double vector, and map_chr() a character vector. The output of .f will be automatically typed upwards, e.g. logical -> integer -> double -> character.

If .x has names(), the return value preserves those names.

walk() returns the input .x (invisibly). This makes it easy to use in pipe.

See also

Other map variants: imap, invoke, lmap, map2, modify

Examples

1:10 %>% map(rnorm, n = 10) %>% map_dbl(mean)
#> [1] 0.9714714 2.3044667 3.3578648 4.0606456 5.2247875 6.6328310 #> [7] 6.6803245 8.1763451 9.2655661 10.2225539
# Or use an anonymous function 1:10 %>% map(function(x) rnorm(10, x))
#> [[1]] #> [1] -0.4960537 -0.1848187 1.6302344 3.1012525 0.3862632 -0.6346383 #> [7] 0.9895589 0.3434939 0.3304666 0.5214110 #> #> [[2]] #> [1] 3.3194563 2.6365628 2.5143278 0.2486249 2.8935975 2.2230384 2.5808166 #> [8] 1.8221786 2.7409667 1.0025569 #> #> [[3]] #> [1] 0.06102244 3.71901566 2.30199496 1.10587416 3.07629925 3.87530850 #> [7] 3.45382739 2.14928309 3.56620161 4.15221195 #> #> [[4]] #> [1] 3.243803 3.510742 2.833948 3.520331 4.115348 2.231952 2.592361 4.709178 #> [9] 2.759157 3.631673 #> #> [[5]] #> [1] 5.462080 4.677167 3.712785 3.969960 6.514089 5.346904 6.779442 5.386631 #> [9] 4.081305 3.415664 #> #> [[6]] #> [1] 5.915941 3.914929 6.003568 5.644229 7.146360 5.778812 7.018179 5.736281 #> [9] 7.658542 5.225913 #> #> [[7]] #> [1] 6.076062 6.724467 6.406600 6.877714 8.179784 7.641037 6.370411 6.192265 #> [9] 6.139510 4.830761 #> #> [[8]] #> [1] 6.624163 7.506868 7.418348 7.832771 8.485993 6.666604 7.738034 8.652386 #> [9] 8.748855 8.896560 #> #> [[9]] #> [1] 10.489300 8.340597 9.537283 9.746803 10.896317 6.939929 9.064544 #> [8] 8.734853 8.552655 7.589299 #> #> [[10]] #> [1] 9.493581 9.730238 8.914845 10.362159 9.664328 11.363804 9.288476 #> [8] 10.662179 10.291130 10.197958 #>
# Or a formula 1:10 %>% map(~ rnorm(10, .x))
#> [[1]] #> [1] -0.2035661 0.9601830 1.6869825 1.7052670 1.9914417 2.1442490 #> [7] -0.2389102 3.6548983 0.8430828 0.5765099 #> #> [[2]] #> [1] 1.8016129 1.1051976 2.9042691 2.0796492 0.7411728 3.0256851 1.2692214 #> [8] 1.8098545 2.5288647 2.5502105 #> #> [[3]] #> [1] 3.5496843 2.3404576 3.0574217 0.1919895 2.0877402 2.2176208 2.3358951 #> [8] 3.6263098 2.4927518 3.2703613 #> #> [[4]] #> [1] 4.467477 4.723995 4.613837 3.382131 4.220725 5.127927 5.813454 3.916174 #> [9] 5.367707 3.372565 #> #> [[5]] #> [1] 4.783371 4.316286 4.555297 5.606490 5.624183 4.304569 4.216361 4.046876 #> [9] 6.792756 5.348977 #> #> [[6]] #> [1] 6.259104 5.194048 6.105665 5.666400 7.641848 5.356094 6.587021 5.849597 #> [9] 4.289178 7.431033 #> #> [[7]] #> [1] 4.354788 5.967543 6.292534 6.299440 7.537885 6.683668 6.160377 5.645072 #> [9] 6.182432 6.365600 #> #> [[8]] #> [1] 8.815949 8.302796 9.807087 7.105973 7.953572 7.528821 7.473307 7.904865 #> [9] 5.504635 8.166889 #> #> [[9]] #> [1] 9.350492 10.433701 9.765907 10.167521 8.863057 8.485098 10.519744 #> [8] 8.671508 8.946328 8.436475 #> #> [[10]] #> [1] 9.256091 9.890958 9.439171 10.188002 10.748851 8.083462 10.236096 #> [8] 10.628953 10.417926 11.976758 #>
# The names of the input are preserved in the output: list(foo = 1, bar = 2) %>% map(`+`, 10)
#> $foo #> [1] 11 #> #> $bar #> [1] 12 #>
# Using set_names() with character vectors is handy to keep track # of the original inputs: set_names(c("foo", "bar")) %>% map_chr(paste0, ":suffix")
#> foo bar #> "foo:suffix" "bar:suffix"
# Extract by name or position # .default specifies value for elements that are missing or NULL l1 <- list(list(a = 1L), list(a = NULL, b = 2L), list(b = 3L)) l1 %>% map("a", .default = "???")
#> [[1]] #> [1] 1 #> #> [[2]] #> [1] "???" #> #> [[3]] #> [1] "???" #>
l1 %>% map_int("b", .default = NA)
#> [1] NA 2 3
l1 %>% map_int(2, .default = NA)
#> [1] NA 2 NA
# Supply multiple values to index deeply into a list l2 <- list( list(num = 1:3, letters[1:3]), list(num = 101:103, letters[4:6]), list() ) l2 %>% map(c(2, 2))
#> [[1]] #> [1] "b" #> #> [[2]] #> [1] "e" #> #> [[3]] #> NULL #>
# Use a list to build an extractor that mixes numeric indices and names, # and .default to provide a default value if the element does not exist l2 %>% map(list("num", 3))
#> [[1]] #> [1] 3 #> #> [[2]] #> [1] 103 #> #> [[3]] #> NULL #>
l2 %>% map_int(list("num", 3), .default = NA)
#> [1] 3 103 NA
# Use a predicate function to decide whether to map a function: map_if(iris, is.factor, as.character)
#> $Sepal.Length #> [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 #> [19] 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 #> [37] 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5 #> [55] 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1 #> [73] 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6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.5 1.9 2.1 1.8 2.2 2.1 1.7 1.8 #> [109] 1.8 2.5 2.0 1.9 2.1 2.0 2.4 2.3 1.8 2.2 2.3 1.5 2.3 2.0 2.0 1.8 2.1 1.8 #> [127] 1.8 1.8 2.1 1.6 1.9 2.0 2.2 1.5 1.4 2.3 2.4 1.8 1.8 2.1 2.4 2.3 1.9 2.3 #> [145] 2.5 2.3 1.9 2.0 2.3 1.8 #> #> $Species #> [1] "setosa" "setosa" "setosa" "setosa" "setosa" #> [6] "setosa" "setosa" "setosa" "setosa" "setosa" #> [11] "setosa" "setosa" "setosa" "setosa" "setosa" #> [16] "setosa" "setosa" "setosa" "setosa" "setosa" #> [21] "setosa" "setosa" "setosa" "setosa" "setosa" #> [26] "setosa" "setosa" "setosa" "setosa" "setosa" #> [31] "setosa" "setosa" "setosa" "setosa" "setosa" #> [36] "setosa" "setosa" "setosa" "setosa" "setosa" #> [41] "setosa" "setosa" "setosa" "setosa" "setosa" #> [46] "setosa" "setosa" "setosa" "setosa" "setosa" #> [51] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [56] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [61] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [66] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [71] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [76] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [81] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [86] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [91] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [96] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [101] "virginica" "virginica" "virginica" "virginica" "virginica" #> [106] "virginica" "virginica" "virginica" "virginica" "virginica" #> [111] "virginica" "virginica" "virginica" "virginica" "virginica" #> [116] "virginica" "virginica" "virginica" "virginica" "virginica" #> [121] "virginica" "virginica" "virginica" "virginica" "virginica" #> [126] "virginica" "virginica" "virginica" "virginica" "virginica" #> [131] "virginica" "virginica" "virginica" "virginica" "virginica" #> [136] "virginica" "virginica" "virginica" "virginica" "virginica" #> [141] "virginica" "virginica" "virginica" "virginica" "virginica" #> [146] "virginica" "virginica" "virginica" "virginica" "virginica" #>
# Specify an alternative with the `.else` argument: map_if(iris, is.factor, as.character, .else = as.integer)
#> $Sepal.Length #> [1] 5 4 4 4 5 5 4 5 4 4 5 4 4 4 5 5 5 5 5 5 5 5 4 5 4 5 5 5 5 4 4 5 5 5 4 5 5 #> [38] 4 4 5 5 4 4 5 5 4 5 4 5 5 7 6 6 5 6 5 6 4 6 5 5 5 6 6 5 6 5 5 6 5 5 6 6 6 #> [75] 6 6 6 6 6 5 5 5 5 6 5 6 6 6 5 5 5 6 5 5 5 5 5 6 5 5 6 5 7 6 6 7 4 7 6 7 6 #> [112] 6 6 5 5 6 6 7 7 6 6 5 7 6 6 7 6 6 6 7 7 7 6 6 6 7 6 6 6 6 6 6 5 6 6 6 6 6 #> [149] 6 5 #> #> $Sepal.Width #> [1] 3 3 3 3 3 3 3 3 2 3 3 3 3 3 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 3 3 3 #> [38] 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 2 2 2 3 2 2 2 2 3 2 2 2 3 3 2 2 2 3 2 2 2 #> [75] 2 3 2 3 2 2 2 2 2 2 3 3 3 2 3 2 2 3 2 2 2 3 2 2 2 2 3 2 3 2 3 3 2 2 2 3 3 #> [112] 2 3 2 2 3 3 3 2 2 3 2 2 2 3 3 2 3 2 3 2 3 2 2 2 3 3 3 3 3 3 3 2 3 3 3 2 3 #> [149] 3 3 #> #> $Petal.Length #> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 #> [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 3 4 3 3 4 4 4 3 4 4 4 4 3 4 4 4 4 #> [75] 4 4 4 5 4 3 3 3 3 5 4 4 4 4 4 4 4 4 4 3 4 4 4 4 3 4 6 5 5 5 5 6 4 6 5 6 5 #> [112] 5 5 5 5 5 5 6 6 5 5 4 6 4 5 6 4 4 5 5 6 6 5 5 5 6 5 5 4 5 5 5 5 5 5 5 5 5 #> [149] 5 5 #> #> $Petal.Width #> [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 #> [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 #> [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 2 1 1 1 2 2 #> [112] 1 2 2 2 2 1 2 2 1 2 2 2 1 2 1 1 1 2 1 1 2 2 1 1 2 2 1 1 2 2 2 1 2 2 2 1 2 #> [149] 2 1 #> #> $Species #> [1] "setosa" "setosa" "setosa" "setosa" "setosa" #> [6] "setosa" "setosa" "setosa" "setosa" "setosa" #> [11] "setosa" "setosa" "setosa" "setosa" "setosa" #> [16] "setosa" "setosa" "setosa" "setosa" "setosa" #> [21] "setosa" "setosa" "setosa" "setosa" "setosa" #> [26] "setosa" "setosa" "setosa" "setosa" "setosa" #> [31] "setosa" "setosa" "setosa" "setosa" "setosa" #> [36] "setosa" "setosa" "setosa" "setosa" "setosa" #> [41] "setosa" "setosa" "setosa" "setosa" "setosa" #> [46] "setosa" "setosa" "setosa" "setosa" "setosa" #> [51] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [56] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [61] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [66] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [71] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [76] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [81] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [86] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [91] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [96] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" #> [101] "virginica" "virginica" "virginica" "virginica" "virginica" #> [106] "virginica" "virginica" "virginica" "virginica" "virginica" #> [111] "virginica" "virginica" "virginica" "virginica" "virginica" #> [116] "virginica" "virginica" "virginica" "virginica" "virginica" #> [121] "virginica" "virginica" "virginica" "virginica" "virginica" #> [126] "virginica" "virginica" "virginica" "virginica" "virginica" #> [131] "virginica" "virginica" "virginica" "virginica" "virginica" #> [136] "virginica" "virginica" "virginica" "virginica" "virginica" #> [141] "virginica" "virginica" "virginica" "virginica" "virginica" #> [146] "virginica" "virginica" "virginica" "virginica" "virginica" #>
# A more realistic example: split a data frame into pieces, fit a # model to each piece, summarise and extract R^2 mtcars %>% split(.$cyl) %>% map(~ lm(mpg ~ wt, data = .x)) %>% map(summary) %>% map_dbl("r.squared")
#> 4 6 8 #> 0.5086326 0.4645102 0.4229655
# Use map_lgl(), map_dbl(), etc to reduce to a vector. # * list mtcars %>% map(sum)
#> $mpg #> [1] 642.9 #> #> $cyl #> [1] 198 #> #> $disp #> [1] 7383.1 #> #> $hp #> [1] 4694 #> #> $drat #> [1] 115.09 #> #> $wt #> [1] 102.952 #> #> $qsec #> [1] 571.16 #> #> $vs #> [1] 14 #> #> $am #> [1] 13 #> #> $gear #> [1] 118 #> #> $carb #> [1] 90 #>
# * vector mtcars %>% map_dbl(sum)
#> mpg cyl disp hp drat wt qsec vs #> 642.900 198.000 7383.100 4694.000 115.090 102.952 571.160 14.000 #> am gear carb #> 13.000 118.000 90.000
# If each element of the output is a data frame, use # map_dfr to row-bind them together: mtcars %>% split(.$cyl) %>% map(~ lm(mpg ~ wt, data = .x)) %>% map_dfr(~ as.data.frame(t(as.matrix(coef(.)))))
#> (Intercept) wt #> 1 39.57120 -5.647025 #> 2 28.40884 -2.780106 #> 3 23.86803 -2.192438
# (if you also want to preserve the variable names see # the broom package) # Use `map_depth()` to recursively traverse nested vectors and map # a function at a certain depth: x <- list(a = list(foo = 1:2, bar = 3:4), b = list(baz = 5:6)) str(x)
#> List of 2 #> $ a:List of 2 #> ..$ foo: int [1:2] 1 2 #> ..$ bar: int [1:2] 3 4 #> $ b:List of 1 #> ..$ baz: int [1:2] 5 6
map_depth(x, 2, paste, collapse = "/")
#> $a #> $a$foo #> [1] "1/2" #> #> $a$bar #> [1] "3/4" #> #> #> $b #> $b$baz #> [1] "5/6" #> #>
# Equivalent to: map(x, map, paste, collapse = "/")
#> $a #> $a$foo #> [1] "1/2" #> #> $a$bar #> [1] "3/4" #> #> #> $b #> $b$baz #> [1] "5/6" #> #>