In a dataframe, I have a particular set of columns (list A) that I want to subtract from another particular set of columns (list B), and then save that output (list A - list B) as columns with the suffix "_diff". Columns from list B have the same names as columns from list A, except they have the suffix "_pop". I'm basically trying to automate a process and avoid manually dividing each column from list A by the correct corresponding column from list B. I have tried experimenting with mutate and across() but I can't get it to work (I think at least one value needs to be fixed like a scalar).
Essentially, if list A contains "Column1A, Column2A, Column3A" and list B contains "Column1B", "Column2B" and "Column3B" (and all of these columns are present in the dataframe), I want to do "Column1A" - "Column1B", "Column2A" - "Column2B", etc., and to have that output saved as new columns with the "_diff" suffix.
This is what I tried, suspecting strongly that it wouldn't work (and it didn't work):
test <- test %>%
mutate(across(my_dataframe[,c(columns_list_A)] - my_dataframe[,c(columns_list_B)], .names="{col}_diff"))
Would the purrr package be a better fit for this problem? I'm not familiar with it, but if someone could point me to the right function I'll be grateful. Thank you very much!