Home > Software design >  Using column name in `dplyr`'s `mutate` function
Using column name in `dplyr`'s `mutate` function

Time:06-13

I want use column's name in mutate function as following:

data = data.frame(x = 1:3, y = 3:1)
data %>% mutate(across(everything() , .fns =list( i_dont_know_what )  , .names =  "m_{col}"))

Result:

  x y m_x m_y
1 1 3   x   y
2 2 2   x   y
3 3 1   x   y

CodePudding user response:

It would be cur_column()

library(dplyr)
data %>%
   mutate(across(everything() , ~ cur_column(), 
     .names =  "m_{.col}"))

-output

  x y m_x m_y
1 1 3   x   y
2 2 2   x   y
3 3 1   x   y
  • Related