I have 18 pairs of variable and I would like to do pair-wise math on them to calculate 18 new variables. The across() function in dplyr is quite handy when applying a formula to one column. Is there a way to apply across() to pairs of columns?
Tiny example with simple division of 2 variables (my actual code will be more complex, some ifelse, ...):
library(tidyverse)
library(glue)
# filler data
df <- data.frame("label" = c('a','b','c','d'),
"A" = c(4, 3, 8, 9),
"B" = c(10, 0, 4, 1),
"error_A" = c(0.4, 0.3, 0.2, 0.1),
"error_B" = c(0.3, 0, 0.4, 0.1))
# what I want to have in the end
# instead of just 2 (A, B), I have 18
df1 <- df %>% mutate(
'R_A' = A/error_A,
'R_B' = B/error_B
)
# what I'm thinking about doing to use both variables A and error_A to calculate the new column
df2 <- df %>% mutate(
across(c('A','B'),
~.x/{HOW DO I USE THE COLUMN WHOSE NAME IS glue('error_',.x)}
.names = 'R_{.col}'
)