I've created a tibble where each column is named to correspond to the arguments of a custom function. There are many columns and so I'm trying to avoid having to explicitly call and assign each argument. I've blindly been trying different syntaxes but to no avail. If some one could help me, I would appreciate it.
Here's a MWE:
# Define simple function
myFunc <- function(a, b, c) a*b^c
# Create tibble of parameters (intentionally out of order)
tbl <- tibble(b = 2:6, a = 1:5, c = -1:3)
# Apply function by listing columns explicitly
tbl |> rowwise() |> mutate(myFunc(a = a, b= b, c=c))
# Fail to apply function...
tbl |> rowwise() |> mutate(myFunc())
# Fail again...
tbl |> rowwise() |> mutate(myFunc(.data))
# Continue ad nauseum...
One option would be to use the purrr::pmap
family of functions, e.g. using pmap_dbl
you could do:
myFunc <- function(a, b, c) a * b^c
library(dplyr, warn.conflicts = FALSE)
library(purrr)
tbl <- tibble(b = 2:6, a = 1:5, c = -1:3)
tbl %>%
mutate(res = pmap_dbl(., myFunc))
#> # A tibble: 5 × 4
#> b a c res
#> <int> <int> <int> <dbl>
#> 1 2 1 -1 0.5
#> 2 3 2 0 2
#> 3 4 3 1 12
#> 4 5 4 2 100
#> 5 6 5 3 1080