map: Apply a function to each element of a list or atomic vector

Description Usage Arguments Value See Also Examples

View source: R/map.R

Description

The map functions transform their input by applying a function to each element of a list or atomic vector and returning an object of the same length as the input.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
map(.x, .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, ...)

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.

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

Value

See Also

map_if() for applying a function to only those elements of .x that meet a specified condition.

Other map variants: imap(), invoke(), lmap(), map2(), map_if(), modify()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Compute normal distributions from an atomic vector
1:10 %>%
  map(rnorm, n = 10)

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

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

# Simplify output to a vector instead of a list by computing the mean of the distributions
1:10 %>%
  map(rnorm, n = 10) %>%  # output a list
  map_dbl(mean)           # output an atomic vector

# Using set_names() with character vectors is handy to keep track
# of the original inputs:
set_names(c("foo", "bar")) %>% map_chr(paste0, ":suffix")

# Working with lists
favorite_desserts <- list(Sophia = "banana bread", Eliott = "pancakes", Karina = "chocolate cake")
favorite_desserts %>% map_chr(~ paste(.x, "rocks!"))

# 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 = "???")
l1 %>% map_int("b", .default = NA)
l1 %>% map_int(2, .default = 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))

# 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))
l2 %>% map_int(list("num", 3), .default = NA)

# Working with data frames
# Use map_lgl(), map_dbl(), etc to return a vector instead of a list:
mtcars %>% map_dbl(sum)

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

# 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(.)))))
# (if you also want to preserve the variable names see
# the broom package)

Example output

[[1]]
 [1]  0.4769785  2.0397388  0.5344131  0.3726848  1.2173322 -0.1736705
 [7]  2.3348791  0.9816031  2.4497055  1.1131854

[[2]]
 [1] 3.193107 2.134278 1.286601 3.149463 1.701752 2.858611 2.569983 2.411019
 [9] 1.491315 2.086711

[[3]]
 [1] 3.726295 3.022633 2.941408 2.236009 2.909115 4.433155 2.526594 2.641556
 [9] 2.161834 2.753321

[[4]]
 [1] 4.443877 4.714776 3.346449 3.696108 5.106824 5.192681 4.200111 3.836659
 [9] 4.840525 2.526162

[[5]]
 [1] 5.554956 5.046872 5.182242 3.113638 6.734541 4.456206 5.313536 3.537841
 [9] 5.388056 4.904613

[[6]]
 [1] 7.255545 6.424624 4.891061 6.703934 7.977434 4.514873 7.202554 6.292616
 [9] 5.838186 5.745246

[[7]]
 [1] 6.235878 6.922850 6.520061 6.781530 8.019793 6.996090 8.223793 6.564179
 [9] 7.370433 8.359766

[[8]]
 [1] 7.723234 8.370644 9.334328 7.646777 7.436890 8.489782 7.380354 7.325526
 [9] 8.197194 6.885721

[[9]]
 [1]  9.231275 10.657107  9.917242  9.651798  9.617321  8.518616 10.494536
 [8]  7.716585  8.399065 11.151865

[[10]]
 [1]  9.749280  8.927761 10.189076 10.547340  9.915421  8.382423  9.496451
 [8] 10.619408 10.518891  9.142343

[[1]]
 [1]  1.7599350  0.9459502  0.7068937 -0.5762349  1.7856794  0.1102035
 [7]  1.4134011  1.5971450  0.8046932  0.9478376

[[2]]
 [1] 1.3286318 2.4112292 2.3514138 0.8336272 2.0010662 1.0764255 1.7381388
 [8] 2.5684965 3.2387754 2.3635423

[[3]]
 [1] 4.195043 3.964948 2.349293 2.890415 2.711107 2.493764 1.826268 1.944809
 [9] 2.928204 3.669702

[[4]]
 [1] 2.805964 5.884557 4.146064 3.836425 3.978837 3.060420 2.665347 3.110497
 [9] 2.460717 4.017245

[[5]]
 [1] 5.433392 5.583858 5.886974 5.631239 5.086561 3.954333 5.034201 3.144799
 [9] 5.655269 4.630076

[[6]]
 [1] 5.587698 5.684845 6.403754 4.664267 5.830739 4.793635 6.932951 5.660232
 [9] 4.222771 7.528149

[[7]]
 [1] 7.440155 6.809808 6.146626 8.564599 8.178589 5.491188 6.481749 7.056656
 [9] 7.382218 7.368242

[[8]]
 [1] 7.389515 9.085676 6.760997 8.496290 7.259518 7.151075 7.071715 9.850365
 [9] 7.631401 9.751613

[[9]]
 [1]  9.097198  9.294229  9.897132 10.012778  9.266967 10.158353 10.153954
 [8]  9.632593  9.130501  8.901136

[[10]]
 [1]  9.717966  8.766000  9.068651  9.301051 11.447465  8.924065  9.338394
 [8] 10.721021 11.273187  9.881974

[[1]]
 [1] -0.09196272 -0.95467618  2.18780040  2.15738982  1.13622845  1.25917113
 [7] -0.38927600  1.57756801 -0.63345867  1.59584914

[[2]]
 [1] 3.5184257 3.4281008 1.0399229 1.1264917 3.9273931 1.6406488 2.4424556
 [8] 1.1748541 3.7576450 0.1577632

[[3]]
 [1] 1.6301637 3.9469834 2.9153626 2.4207850 2.2825866 4.0284972 2.1227454
 [8] 2.1783485 2.5822347 0.6607519

[[4]]
 [1] 4.195687 2.426989 3.942000 4.581475 4.944939 4.221556 3.849210 4.890036
 [9] 5.293970 3.730568

[[5]]
 [1] 3.722894 4.519406 3.379876 4.134200 4.257726 5.766882 5.193027 4.322917
 [9] 3.966166 3.807681

[[6]]
 [1] 5.909763 5.867640 5.653034 6.404426 5.649775 7.347084 6.071290 7.413239
 [9] 6.356672 4.028788

[[7]]
 [1] 8.310163 6.213613 7.234469 8.414722 8.294896 6.126435 7.690544 9.148273
 [9] 8.696620 6.915747

[[8]]
 [1] 6.413971 7.893909 8.715056 8.845230 6.736224 8.913889 7.052592 7.091206
 [9] 8.246947 8.749093

[[9]]
 [1]  9.736420  8.698617 10.033653  9.613345  8.088434 10.632552  9.452494
 [8] 10.224751 10.557451  8.586611

[[10]]
 [1] 10.781892 10.227076  9.891581 10.763654 10.307383  9.923994  9.840969
 [8] 10.220118  9.239497 10.214427

 [1] 0.5465493 2.0444656 3.0101870 4.4019210 4.6715143 6.2488165 7.2231565
 [8] 7.9605995 8.6737348 9.8966895
         foo          bar 
"foo:suffix" "bar:suffix" 
                 Sophia                  Eliott                  Karina 
  "banana bread rocks!"       "pancakes rocks!" "chocolate cake rocks!" 
[[1]]
[1] 1

[[2]]
[1] "???"

[[3]]
[1] "???"

[1] NA  2  3
[1] NA  2 NA
[[1]]
[1] "b"

[[2]]
[1] "e"

[[3]]
NULL

[[1]]
[1] 3

[[2]]
[1] 103

[[3]]
NULL

[1]   3 103  NA
     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 
        4         6         8 
0.5086326 0.4645102 0.4229655 
  (Intercept)        wt
1    39.57120 -5.647025
2    28.40884 -2.780106
3    23.86803 -2.192438

purrr documentation built on April 19, 2020, 4:17 p.m.