The function my_function takes parameter stored in a data frame. parameters and take one extra parameter as another data frame independently in indf
library(tidyverse)
my_function <- function (x=NULL,y=NULL,z=NULL, indf=NULL) {
out <- (x * y *z )
out * indf
}
parameters <- tribble(
~x, ~y, ~z,
5, 1, 1,
10, 5, 3,
-3, 10, 5
)
indf <- tribble(
~A, ~B, ~C,
100, 10, 1,
1000, 300, 3,
20, 10, 5
)
parameters %>%
pmap(my_function, indf=indf)
The output shows the list of data frames
#> [[1]]
#> A B C
#> 1 500 50 5
#> 2 5000 1500 15
#> 3 100 50 25
#>
#> [[2]]
#> A B C
#> 1 15000 1500 150
#> 2 150000 45000 450
#> 3 3000 1500 750
#>
#> [[3]]
#> A B C
#> 1 -15000 -1500 -150
#> 2 -150000 -45000 -450
#> 3 -3000 -1500 -750
When I run the above function with parallel package using the following code:
library(parallel)
parameters %>%
lift(mcmapply, mc.cores = detectCores() - 1)(FUN = my_function, indf=indf)
The following matrix is produced.
[,1] [,2] [,3]
[1,] 500 1500 -150
[2,] 5000 45000 -450
[3,] 100 1500 -750
How can I implement parallel so that it produces a list of data frames like the initial output?