I am trying to apply a function to pairs of columns in dplyr
. In the simple example below, there are columns a
and b
, as well as a_exp
and b_exp
. I want to create a new column called a_mul = a*a_exp
and another one for b (and c, d, e ...). I know I can solve this problem with a loop and a function, with purrr
, or with pivot_longer
, but I want to know if I can solve it with mutate(across())
. This is a minimal example. The real problem is more complicated, so I can't create both columns in one go because in the real case a_exp
is not a function of a
. I am trying the following code, and I get
"promise already under evaluation: recursive default argument reference or earlier problems?"
I don't understand why or how to fix it.
test <- tibble(a = runif(10), b = runif(10)) %>%
mutate(across(c(a,b), exp, .names = '{.col}_exp'))
test <- test %>%
mutate(across(c(a,b),
~ .x * .data[[paste0(cur_column(), '_exp')]],
.names = '{.col}_mult'))