I'm trying to use the pmap function from the purrr package to loop over different columns of a tibble. Each column contains certain model parameters (e.g. sample size, coefficients, etc.) that I would like to use as input for my function. Each row belongs to one simulated model. For this, I would like to tell pmap directly to which column the arguments ..1, ..2, etc. should refer to by using the names of those columns. However, I'm struggling with that.
For example, the following code would generate three uniform distributions, whereby each row defines the sample size, min and max value of the distribution.
set.seed(2022)
test <- tibble(n = c(10, 20, 30),
min = c(0, 100, 500),
max = c(100, 500, 1000))
test %>% pmap(..1 = n, ..2 = min, ..3 = max, ~runif(n = ..1, min = ..2, max = ..3) %>% round(digits = 0))
The above code runs. However, it seems that explicitly assigning ..1 = n, ..2 = min, etc. in the first part of the pmap function has no effect. Rather, the arguments ..1, ..2, and ..3 seem to refer to the position of the actual columns in the data frame. Leaving out that first part yields the same results
set.seed(2022)
test %>% pmap(~runif(n = ..1, min = ..2, max = ..3) %>% round(digits = 0))
This becomes problematic and prone to errors when the data frame has many columns or the order of the columns changes. For example, for the same df (test2) with two additional columns, the above code will throw an error as column 2 has now been replaced with a character column and in addition columns 1 and 3 now also refer to something different.
test2 <- tibble(model = (1:3),
type_dist= rep("uniform", 3),
n = c(10, 20, 30),
min = c(0, 100, 500),
max = c(100, 500, 1000))
set.seed(2022)
test2 %>% pmap(..1 = n, ..2 = min, ..3 = max, ~runif(n = ..1, min = ..2, max = ..3) %>% round(digits = 0))
Error in runif(n = ..1, min = ..2, max = ..3) : invalid arguments
It does not matter that I try to assign the ..1, ..2, and ..3 explicitly to the column names. Instead, I would have to make sure that I now refer to columns 3 to 5.
set.seed(2022)
test2 %>% pmap(~runif(n = ..3, min = ..4, max = ..5) %>% round(digits = 0))
With multiple data frames or a larger number of columns in varying order, it is quite easy to mess this up somewhere, e.g. by confusing the order of the parameters.
So my question: Is it possible to explicitly assign the ..1, ..2, ..3, ... arguments that pmap uses to a certain column by its column name, rather than its position in the data frame?