OK got it. I add something @hadley cooked up in another channel that is quite nice. He uses list()
as the first argument of the pmap_()
, instead of .
, to select the relevant columns of the data frame. This also means you can re-associate variable names to argument names on-the-fly, as we need to do here.
library(tidyverse)
set.seed(42)
df <- tribble(
~groupA, ~groupB, ~v1, ~v2,
"A","C",4, 1,
"A","D",2, 3,
"A","D",1, 5
)
fun <- function(x, y) {
val <- sum(rnorm(10,x,y))
return(val)
}
df %>%
mutate(t = pmap_dbl(list(x = v1, y = v2), fun))
#> # A tibble: 3 x 5
#> groupA groupB v1 v2 t
#> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 A C 4 1 45.5
#> 2 A D 2 3 15.1
#> 3 A D 1 5 1.10