I would like to correct a column in a data.frame by subtracting from it another column with nearly identical name, but this other column has a suffix. I would like to use the mutate_at function for this.
Trying to figure this out, I have struggled to access the name of a column in the function part of mutate_at, to the use it to access the other column.
I show this in a small example below, but basically I would like to access the name of the column used at the moment . and then select from the data in the pipe a column that has the same name as . but with a suffix (below that would be "_new").
Thanks for your help!
Here is an example of how I would have liked to do it - but this does not work.
library(tidyverse)
data("mtcars")
new <- mtcars/4
names(new) <-paste0(names(new),"_new")
df <- bind_cols(mtcars,new)
df %>%
mutate_at(.vars = vars(carb,disp),
.funs = list(corrected = ~ . - df %>% pull(paste0(names(.),"_new"))))
df %>% pull(paste0("carb","_new"))