Apply a function to each element of a vector

map() returns the transformed input; walk() calls .f for its side-effect and returns the original 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_df() returns a data frame by row-binding the individual elements.

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

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

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

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

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

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

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


A list or atomic vector.


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.


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


Note that map() understands data frames, including grouped data frames. It can be much faster than mutate_each() when your data frame has many columns. However, map() will be slower for the more common case of many groups with functions that dplyr knows how to translate to C++.


map() always returns a list.

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

walk() (invisibly) the input .x. It's called primarily for its side effects, but this makes it easier to combine in a pipe.

See Also

map2() and pmap() to map over multiple inputs simulatenously

  • map
  • map_lgl
  • map_chr
  • map_int
  • map_dbl
  • map_df
  • walk
1:10 %>%
  map(rnorm, n = 10) %>%

# Or use an anonymous function
1:10 %>%
  map(function(x) rnorm(10, x))

# Or a formula
1:10 %>%
  map(~ rnorm(10, .x))

# 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) %>%

# Use map_lgl(), map_dbl(), etc to reduce to a vector.
# * list
mtcars %>% map(sum)
# * vector
mtcars %>% map_dbl(sum)

# 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)) %>%
# (if you also want to preserve the variable names see
# the broom package)
# }
Documentation reproduced from package purrr, version, License: GPL-3 | file LICENSE

Community examples at May 22, 2019 purrr v0.2.5

Expanding on the first example. Is it specified what the `.x` argument to `map` refers to when `.f` takes multiple arguments? ```{r} map(1:10,rnorm,mean=5) # length of vector is what ranges from 1 to 10, mean is 5 ``` ```{r} map(1:10,rnorm,n=20,mean=5) # sd is what ranges from 1 to 10 ```