10 Map with multiple inputs

In the previous purrr units, you learned how to use the map() functions to iterate over a single vector and apply a function to each element. purrr also contains functions that can iterate over several vectors in parallel, supplying the first elements of each vector to a given function, then the second, then the third, etc.

purrr’s parallel mapping functions allow the assembly line to have multiple, synchronized input conveyor belts. Our factory worker uses the nth item from each input conveyor belt to create a new object that becomes the nth item on the output conveyor belt.

Below, you’ll learn about the map2() functions, which can handle two input vectors, and the pmap() functions, which can handle any number of input vectors.

10.1 map2()

The map2() functions are very similar to the map() functions you learned about previously, but they take two input vectors instead of one.

For example, here are two vectors, x and y.

We can use a map2() variant to iterate along both vectors in parallel. The following code creates a new vector whose first element is the minimum of x[1] and y[1], second element is the minimum of x[2] and y[2], and third element is the minimum of x[3] and y[3].

Since the map2() functions iterate along the two vectors in parallel, they need to be the same length.

Inside anonymous functions in the map() functions, you refer to each element of the input vector as . . In the map2() functions, you refer to elements of the first vector as .x and elements of the second as .y .

If you don’t create an anonymous function and use a named function instead, the first vector will be supplied as the first argument to the function and the second vector will be supplied as the second argument.

Remember that tibble columns are vectors, so you can use map2() inside mutate() to alter tibble columns.

10.2 pmap()

There are no map3() or map4() functions. Instead, you can use a pmap() (p for parallel) function to map over more than two vectors.

The pmap() functions work slightly differently than the map() and map2() functions. In map() and map2() functions, you specify the vector(s) to supply to the function. In pmap() functions, you specify a single list that contains all the vectors (or lists) that you want to supply to your function.

Flipping the list diagram makes it easier to see that pmap() is basically just a generalized version of map2().

The only difference is that map2() lets you specify each vector as a separate argument. In pmap(), you have to store all your input vectors in a single list. This functionality allows pmap() to handle any number of input vectors. Here’s our earlier map2() statement.

To do this in pmap(), just create a list out of x and y.

If you only have two input vectors, though, use map2(). If we want to apply min() to parallel elements of three vectors, we’ll need to use pmap().

z is a third vector.

Again, we need to combine all the individual vectors into a single list in order to use pmap().

Tibbles are lists, so we could also combine x, y, and z into a tibble.

10.2.2 Named functions

If you supply pmap() a named function, it will match the names of the input list with the names of the function arguments. This can result in elegant code. But for this to work, it’s important that:

  • The list or tibble input variable names match those of the function arguments.
  • You have the same number of input variables as function arguments.

Let’s start with an example of what doesn’t work. First, we’ll create a named function.

This does not work:

state_animals has four variables, but state_sentence is expecting three. The number of input variables must match the number of function arguments.

The easiest way to fix the problem is to just get rid of the unused variable.

Note that the order of the variables in state_animals is different than the order of the arguments in state_sentence. pmap() matches input variables with function arguments by name, so the orderings don’t matter. However, this means that the two sets of names must be identical.