Examples for 'purrr::map'


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

Aliases: map map_lgl map_chr map_int map_dbl map_raw map_dfr map_df map_dfc walk

Keywords:

### ** Examples

# Compute normal distributions from an atomic vector
1:10 %>%
  map(rnorm, n = 10)
[[1]]
 [1]  1.9387102  0.8783509  0.7880443 -0.3155046  1.4807586  1.3074779
 [7]  0.5600396  1.4540053 -0.1517757 -0.1599775

[[2]]
 [1] 1.622899 2.101425 3.048021 1.480330 3.943505 2.291922 2.574481 3.176467
 [9] 2.846962 2.608265

[[3]]
 [1] 4.222034 2.575776 2.394766 2.714443 4.043980 3.157874 3.869995 2.730669
 [9] 1.809008 2.191912

[[4]]
 [1] 2.742911 4.931114 4.085140 5.252781 4.422800 2.458722 2.305174 4.207963
 [9] 5.808217 4.747124

[[5]]
 [1] 7.238184 5.111757 4.329095 4.512909 4.417067 5.974941 3.670315 5.968359
 [9] 4.121283 6.083598

[[6]]
 [1] 7.009298 5.070858 2.721285 5.785073 5.640342 5.643593 5.241538 5.428374
 [9] 7.146001 5.806140

[[7]]
 [1] 7.270316 8.272042 6.572582 8.751402 7.598013 7.268362 6.576031 6.624514
 [9] 6.535201 6.252226

[[8]]
 [1] 8.393489 7.188083 8.427215 7.822550 7.602798 8.791938 9.651898 8.006470
 [9] 7.632077 6.799031

[[9]]
 [1] 8.706301 9.076514 9.617870 6.851333 9.150723 8.743793 8.976470 8.675278
 [9] 9.092365 9.602405

[[10]]
 [1] 11.587027  9.886389  9.461007 10.096256  9.478319 10.546169 11.267051
 [8]  7.894725 10.788795  9.827902
# You can also use an anonymous function
1:10 %>%
  map(function(x) rnorm(10, x))
[[1]]
 [1]  3.11999259  0.47059680 -0.07146338  1.71391817  1.38262048  1.48906002
 [7]  1.29743397  1.73824037 -0.33413059  1.86265610

[[2]]
 [1]  1.6860447  2.0945895  2.6860537  1.0232517  1.4628065 -0.2215824
 [7]  2.5866000 -0.3633533  2.0805896  2.4585248

[[3]]
 [1] 3.740159 1.929091 2.337720 3.015396 3.136365 2.990526 4.260697 1.729353
 [9] 3.127813 3.725726

[[4]]
 [1] 4.520864 4.408656 2.949166 3.220031 3.528060 3.091445 3.187629 4.674529
 [9] 2.914491 4.126285

[[5]]
 [1] 5.852932 5.024162 4.882690 4.598430 5.527226 4.639729 5.460618 6.444008
 [9] 4.588359 4.870138

[[6]]
 [1] 5.847773 4.678494 6.245596 6.343604 4.103371 8.166449 4.383253 5.708647
 [9] 6.693966 5.045618

[[7]]
 [1] 7.979777 6.379227 5.643726 6.800490 6.527120 7.381632 5.674401 7.686038
 [9] 6.703946 7.833041

[[8]]
 [1] 7.720471 7.266269 7.329517 7.784647 7.997420 6.448698 8.250119 7.597152
 [9] 7.001408 6.461876

[[9]]
 [1]  9.496748  8.424388  8.737797  7.948510  7.919841  8.439048  7.985533
 [8] 10.213425  8.285159  8.167743

[[10]]
 [1] 10.219642  7.599881 10.209732  9.933109  8.530547 10.381494  9.677473
 [8] 12.239091  9.484121  9.228268
# Or a formula
1:10 %>%
  map(~ rnorm(10, .x))
[[1]]
 [1]  0.4317073  0.4033545 -0.5392183  1.5412531  1.8638100  0.2732892
 [7]  1.5872873 -0.1735912 -0.7542432  0.4689713

[[2]]
 [1] 1.3393881 2.5364489 2.2841910 2.1101714 1.1538849 3.8624358 1.6589356
 [8] 0.8125566 1.2249548 1.4479032

[[3]]
 [1] 2.620370 3.153807 2.840385 2.044355 3.706701 3.188491 2.641205 3.980515
 [9] 3.046637 4.109095

[[4]]
 [1] 4.361393 3.775508 4.710337 3.051586 4.519851 3.108516 3.804205 5.382209
 [9] 3.562163 4.429751

[[5]]
 [1] 5.914777 5.894766 6.123667 4.275260 6.722845 3.763689 6.043463 4.799944
 [9] 4.850801 4.269569

[[6]]
 [1] 5.103872 4.183706 6.538752 7.011154 5.817356 6.523622 6.602015 6.878266
 [9] 5.657610 7.103528

[[7]]
 [1] 6.326683 5.963478 5.765856 7.666260 7.195439 6.998480 6.118748 7.208095
 [9] 7.941284 7.546437

[[8]]
 [1] 6.875504 7.283789 7.483820 8.100358 7.592542 8.518645 7.903434 8.770284
 [9] 9.042533 7.190983

[[9]]
 [1]  8.179616  9.273305  8.009386  9.120436  9.484311  9.853318  8.236515
 [8]  7.275315 10.811816  9.021189

[[10]]
 [1]  9.188351 10.689925  9.245590  9.343178 10.905972  9.643291  9.350801
 [8] 10.349509 11.665544 10.421839
# 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
 [1] 1.370136 1.870694 2.860474 4.405303 5.129947 6.121721 6.742919 7.555540
 [9] 8.692043 9.612160
# 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" 
# Working with lists
favorite_desserts <- list(Sophia = "banana bread", Eliott = "pancakes", Karina = "chocolate cake")
favorite_desserts %>% map_chr(~ paste(.x, "rocks!"))
                 Sophia                  Eliott                  Karina 
  "banana bread rocks!"       "pancakes rocks!" "chocolate cake 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 = "???")
[[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
# Working with data frames
# Use map_lgl(), map_dbl(), etc to return a vector instead of a list:
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 
# 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 
# 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)

[Package purrr version 0.3.4 Index]