The map function transform the input, returning a vector the same length as the input. map() returns a list or a data frame; map_lgl(), map_int(), map_dbl() and map_chr() return vectors of the corresponding type (or die trying); 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_lgl(.x, .f, ...)

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

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

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

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

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

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

Arguments

.x

A list or atomic vector.

.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. 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. Within a list, wrap strings in get_attr() to extract named attributes. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to .f.

.id

If not NULL a variable with this name will be created giving either the name or the index of the data frame.

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. 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 #>
# 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
# 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_df to row-bind them together: mtcars %>% split(.$cyl) %>% map(~ lm(mpg ~ wt, data = .x)) %>% map_df(~ 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)