Sam
Sam

Reputation: 89

purrr::pmap for functions with multiple inputs and multiple return values

I am trying to set up a customized function with multiple inputs and multiple return values, and using this function with purrr::map on a data frame.

my sample data is:

test_data <- 
      tibble(x1 = 1:10, 
             x2 = 2:11,
             x3 = 3:12,
             x4 = x1 + x2 + x3)

this test_data looks like this:

# A tibble: 10 x 4
      x1    x2    x3    x4
   <int> <int> <int> <int>
 1     1     2     3     6
 2     2     3     4     9
 3     3     4     5    12
 4     4     5     6    15
 5     5     6     7    18
 6     6     7     8    21
 7     7     8     9    24
 8     8     9    10    27
 9     9    10    11    30
10    10    11    12    33

Firstly, if my function only has one return value (output_3 in this case):

my_function_1 <- 
  function(var1, var2, var3, var4){
    output_1 <- var1 + var2
    output_2 <- var2 + var3 
    output_3 <- var1 + var2 + var3
    output_4 <- var1 + var2 + var4
    return(output_3)
  }

I cam pmap this function using

my_results <-
  dplyr::as.tbl(test_data) %>% 
  dplyr::mutate(output = purrr::pmap(list(var1 = x1, var2 = x2, var3 = x3, var4 = x4),
                                     my_function_1)) %>% 
  tidyr::unnest()

the results looks like this:

 my_results 
# A tibble: 10 x 5
      x1    x2    x3    x4 output
   <int> <int> <int> <int>  <int>
 1     1     2     3     6      6
 2     2     3     4     9      9
 3     3     4     5    12     12
 4     4     5     6    15     15
 5     5     6     7    18     18
 6     6     7     8    21     21
 7     7     8     9    24     24
 8     8     9    10    27     27
 9     9    10    11    30     30
10    10    11    12    33     33

Now if my function has more than one return values, like

my_function_2 <- 
  function(var1, var2, var3, var4){
    output_1 <- var1 + var2
    output_2 <- var2 + var3 
    output_3 <- var1 + var2 + var3
    output_4 <- var1 + var2 + var4
    return(list(output_1, output_2, output_3, output_4))
  }

How should I map this my_function_2 with purrr::map and add return columns to test_data, just like previous step with one return value?

I am also thinking to only have output results first (using the following code), and then join/bind with test_data:

pmap(list(test_data$x1,
              test_data$x2, 
              test_data$x3, 
              test_data$x4),
             my_function_2) %>% 
  flatten()

But the results is not in wanted format, like the following:

[[1]]
[1] 3

[[2]]
[1] 5

[[3]]
[1] 6

[[4]]
[1] 9

[[5]]
[1] 5
... ...

Could anybody remind me some potential solution to format the outputs and join with original test_data?

Upvotes: 2

Views: 2545

Answers (3)

moodymudskipper
moodymudskipper

Reputation: 47330

In your example the computations are vectorized so you don't need pmap and we can do the following :

library(tidyverse)
test_data %>% 
  mutate(!!!setNames(invoke(my_function_2,unname(.)),paste0("output_",1:4)))
# # A tibble: 10 x 8
#       x1    x2    x3    x4 output_1 output_2 output_3 output_4
#    <int> <int> <int> <int>    <int>    <int>    <int>    <int>
#  1     1     2     3     6        3        5        6        9
#  2     2     3     4     9        5        7        9       14
#  3     3     4     5    12        7        9       12       19
#  4     4     5     6    15        9       11       15       24
#  5     5     6     7    18       11       13       18       29
#  6     6     7     8    21       13       15       21       34
#  7     7     8     9    24       15       17       24       39
#  8     8     9    10    27       17       19       27       44
#  9     9    10    11    30       19       21       30       49
# 10    10    11    12    33       21       23       33       54

If you name your elements inside of my_function_2 (the simplest way is to use dplyr::lst instead of list it is even simpler:

my_function_2 <- 
  function(var1, var2, var3, var4){
    output_1 <- var1 + var2
    output_2 <- var2 + var3 
    output_3 <- var1 + var2 + var3
    output_4 <- var1 + var2 + var4
    return(lst(output_1, output_2, output_3, output_4))
  }


test_data %>% 
  mutate(!!!invoke(my_function_2,unname(.)))
# # A tibble: 10 x 8
#       x1    x2    x3    x4 output_1 output_2 output_3 output_4
#    <int> <int> <int> <int>    <int>    <int>    <int>    <int>
#  1     1     2     3     6        3        5        6        9
#  2     2     3     4     9        5        7        9       14
#  3     3     4     5    12        7        9       12       19
#  4     4     5     6    15        9       11       15       24
#  5     5     6     7    18       11       13       18       29
#  6     6     7     8    21       13       15       21       34
#  7     7     8     9    24       15       17       24       39
#  8     8     9    10    27       17       19       27       44
#  9     9    10    11    30       19       21       30       49
# 10    10    11    12    33       21       23       33       54

Or if you need to use pmap because you use unvectorized operations in your real case :

test_data %>% 
  mutate(!!!pmap_dfr(unname(.),my_function_2))

Upvotes: 1

akrun
akrun

Reputation: 887531

A better option would be to create the return value as a tibble in the function and then just apply the pmap

library(purrr)
library(dplyr)
my_function_2 <- 
  function(var1, var2, var3, var4){
    output_1 <- var1 + var2
    output_2 <- var2 + var3 
    output_3 <- var1 + var2 + var3
    output_4 <- var1 + var2 + var4
    tibble::tibble(output_1, output_2, output_3, output_4)
  }

pmap_dfr(list(test_data$x1,
               test_data$x2, 
               test_data$x3, 
               test_data$x4),
              my_function_2) %>%
    bind_cols(test_data, .)
# A tibble: 10 x 8
#      x1    x2    x3    x4 output_1 output_2 output_3 output_4
#   <int> <int> <int> <int>    <int>    <int>    <int>    <int>
# 1     1     2     3     6        3        5        6        9
# 2     2     3     4     9        5        7        9       14
# 3     3     4     5    12        7        9       12       19
# 4     4     5     6    15        9       11       15       24
# 5     5     6     7    18       11       13       18       29
# 6     6     7     8    21       13       15       21       34
# 7     7     8     9    24       15       17       24       39
# 8     8     9    10    27       17       19       27       44
# 9     9    10    11    30       19       21       30       49
#10    10    11    12    33       21       23       33       54

Also, if the column names match the arguments of the function, we don't need to call each column separately

pmap_dfr(set_names(test_data, paste0("var", 1:4)), my_function_2) %>% 
           bind_cols(test_data, .)

Upvotes: 5

Ronak Shah
Ronak Shah

Reputation: 389155

One option is to return a vector from the function

my_function_2 <- function(var1, var2, var3, var4){
    output_1 <- var1 + var2
    output_2 <- var2 + var3 
    output_3 <- var1 + var2 + var3
    output_4 <- var1 + var2 + var4
    return(c(output_1, output_2, output_3,  output_4))
}

and then use pmap_dfc and cbind to original dataframe

library(tidyverse)

bind_cols(test_data, 
 pmap_dfc(list(test_data$x1,
               test_data$x2, 
               test_data$x3, 
               test_data$x4),
               my_function_2) %>% t() %>% data.frame() %>%
 set_names(paste0("x", 5:8)))


# A tibble: 10 x 8
#      x1    x2    x3    x4    x5    x6    x7    x8
#   <int> <int> <int> <int> <int> <int> <int> <int>
# 1     1     2     3     6     3     5     6     9
# 2     2     3     4     9     5     7     9    14
# 3     3     4     5    12     7     9    12    19
# 4     4     5     6    15     9    11    15    24
# 5     5     6     7    18    11    13    18    29
# 6     6     7     8    21    13    15    21    34
# 7     7     8     9    24    15    17    24    39
# 8     8     9    10    27    17    19    27    44
# 9     9    10    11    30    19    21    30    49
#10    10    11    12    33    21    23    33    54

Upvotes: 1

Related Questions