Apply a Function to Each Element of Two Inputs

TL;DR map2(.x, .y, .f) is very similar to map(.x, .f), but applies the operation .f to elements in both inputs of .x and .y, and returns a list. The map2_*() functions, e.g., map2_lgl(), map2_dbl(), map2_int() and map2_chr(), works in a similar way as map2() but returns a vector of the indicated type.


If you are not familar with map() yet, I recommend you to first check it out, and it’ll make it much easier to understand map2().

The map2() function applies a function to each element of two paired inputs (vector or list). The basic argument structure follows map(.x, .y, .f) (as in other functions in the map2_* family):

  • .x and .y: vector or list. The length of the two inputs should be the same; or one input is of length-one, and recycled to the length of the other (i.e., the rule of tidyverse recycle).
  • .f defines the function to be applied to each paired elements in .x and .y. It is in the format of a named function (without quote), e.g. sum; or an anonymous function, e.g., ~ sum(.x, .y), ~ .x + .y, \(x, y) x + y, and function(x, y) x + y.
  • The output is a list of the same length as the input.

If the .f - specified operation that is applied to each paired elements of .x and .y returns a vector of length-one, then you can also output the result as a vector of a specific type, instead of as a list.

  • map2_dbl() returns a vector of type of double (i.e., numeric).
  • map2_lgl() returns a vector of type of logic.
  • map2_int() returns a vector of type of integer.
  • map2_chr() returns a vector of type of character.

e.g.1. Calculate A raised to the power of B on a paired basis.

library(purrr)library(dplyr)
A <- c(1, 2, 4)B <- c(0, 1, 2)
map2(A, B, \(x, y) x ^ y )

Output:

[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 16

This can be written in the following equivalent forms:

map2(A, B, function(x, y) x ^ y )map2(A, B, ~ .x ^ .y)

Return the output as an integer vector.

map2_int(A, B, ~ .x ^ .y)

Output:

[1] 1 2 16

Return the output as a character vector.

map2_chr(A, B, ~ as.character(.x ^ .y) )

Output:

[1] "1" "2" "16"

e.g.2. The following code creates three vectors (of a list) of a normal distribution, with 1, 5, and 10 as the mean, and 0.1, 1, and 5 as the associated sd, respectively.

myMean <- c(1,  5, 10)mySD   <- c(.1, 1, 5)
map2( .x = myMean, .y = mySD, .f = \(x, y) rnorm(n = 10, mean = x, sd = y) %>% round(1))

Output:

[[1]]
[1] 1.0 1.0 1.0 0.9 1.2 1.0 0.9 0.9 1.2 0.8
[[2]]
[1] 4.9 6.1 4.3 5.8 4.9 4.2 5.3 4.0 3.6 2.6
[[3]]
[1] 13.7 8.4 18.7 17.2 7.4 16.9 7.2 1.5 14.2 6.4

The .f argument can be equivalently written in the following way:

map2(.x = myMean, .y = mySD,     .f = function(x, y) rnorm(n = 10, mean = x, sd = y) %>% round(1)) 
map2(.x = myMean, .y = mySD, .f = ~ rnorm(n = 10, mean = .x, sd = .y) %>% round(1))

e.g.3 Using the iris dataset, and separately for each species, here we create a linear model between Sepal.Length and Sepal.Width (using the map() function as covered in the last section), and then predict the Sepal.Length based on the observed Sepal.Width.

# split the `iris` dataset into a list of data frames# with each element (data frame) being a subset of a 'Species' type iris.list <- iris %>% split(iris$Species)
# separately for each species (element of the list), # create a linear model of Sepal.Length (response) vs. Sepal.Width (feature)mods <- iris.list %>% map(~lm(Sepal.Length ~ Sepal.Width, data = .x))
# separately for each species# predict 'Sepal.Length' based on the observed Sepal.Widthmap2(mods, iris.list, predict)

Output:

$setosa
1 2 3 4 5 6 7 8
5.055715 4.710470 4.848568 4.779519 5.124764 5.331911 4.986666 4.986666
9 10 11 12 13 14 15 16
4.641421 4.779519 5.193813 4.986666 4.710470 4.710470 5.400960 5.677156
17 18 19 20 21 22 23 24
5.331911 5.055715 5.262862 5.262862 4.986666 5.193813 5.124764 4.917617
25 26 27 28 29 30 31 32
4.986666 4.710470 4.986666 5.055715 4.986666 4.848568 4.779519 4.986666
33 34 35 36 37 38 39 40
5.470009 5.539058 4.779519 4.848568 5.055715 5.124764 4.710470 4.986666
41 42 43 44 45 46 47 48
5.055715 4.227128 4.848568 5.055715 5.262862 4.710470 5.262862 4.848568
49 50
5.193813 4.917617 $versicolor
51 52 53 54 55 56 57 58
6.307983 6.307983 6.221476 5.529413 5.961952 5.961952 6.394491 5.615921
59 60 61 62 63 64 65 66
6.048460 5.875445 5.269890 6.134968 5.442906 6.048460 6.048460 6.221476
67 68 69 70 71 72 73 74
6.134968 5.875445 5.442906 5.702429 6.307983 5.961952 5.702429 5.961952
75 76 77 78 79 80 81 82
6.048460 6.134968 5.961952 6.134968 6.048460 5.788937 5.615921 5.615921
83 84 85 86 87 88 89 90
5.875445 5.875445 6.134968 6.480999 6.221476 5.529413 6.134968 5.702429
91 92 93 94 95 96 97 98
5.788937 6.134968 5.788937 5.529413 5.875445 6.134968 6.048460 6.048460
99 100
5.702429 5.961952 $virginica
101 102 103 104 105 106 107 108
6.881900 6.340980 6.611440 6.521286 6.611440 6.611440 6.160673 6.521286
109 110 111 112 113 114 115 116
6.160673 7.152361 6.791747 6.340980 6.611440 6.160673 6.431133 6.791747
117 118 119 120 121 122 123 124
6.611440 7.332667 6.250826 5.890212 6.791747 6.431133 6.431133 6.340980
125 126 127 128 129 130 131 132
6.881900 6.791747 6.431133 6.611440 6.431133 6.611440 6.431133 7.332667
133 134 135 136 137 138 139 140
6.431133 6.431133 6.250826 6.611440 6.972054 6.701593 6.611440 6.701593
141 142 143 144 145 146 147 148
6.701593 6.701593 6.340980 6.791747 6.881900 6.611440 6.160673 6.611440
149 150
6.972054 6.611440