dplyr: access column name in mutate_at function

Moritz Schwarz

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"))

Abdessabour Mtk

Instead of using mutate_at why not use mutate combined with across and cur_column i.e:

df %>% 
  mutate( across( c(carb,disp), ~ . - pull(df, paste0(cur_column(), "_new") ),  .names = "{.col}_corrected") )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using column name in `dplyr`'s `mutate` function

dplyr column selection with placeholder . and paste in mutate_at

Using dplyr mutate_at with custom function

dplyr::mutate_at iterate through columns in function

Using dplyr quosure custom function with mutate_at

dplyr mutate_at function applied to multiple columns - using dynamic column names

Write a function with default column name inputs in dplyr::mutate()

Dplyr mutate minimum column name

Pass function arguments by column position to mutate_at

Exclude column in `dplyr` `mutate_at` while using data in this column

Mutate with a list column function in dplyr

How can I access a column name within a function in a mutate call?

pass a column name to a function using dplyr mutate without using the depreciated mutate_

opposite of mutate_at dplyr

opposite of mutate_at in dplyr

Update cell values with the column name using mutate_at

How to use the name of the column I'm currently using in a mutate_at?

Access the column names in the `mutate_at` to use it for subseting a list

In R: pass column name as argument and use it in function with dplyr::mutate() and lazyeval::interp()

dynamicaly name a new variable / column within a custom function dplyr mutate and paste

dplyr mutate, custom function and variable name as characters

How to pass column name as parameter to function in dplyr?

dplyr: passing column name to summarize inside function

mutate_at function cancels the previous mutate_at

mutate_ column using a calculated column name in dplyr

dplyr mutate_at and ifelse() is not vectorised

dplyr::mutate_at for changing prefixes?

using mutate_at from dplyr

Mutate_if or mutate_at in dplyr with Dates