Iman
Iman

Reputation: 2324

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

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

Upvotes: 2

Views: 451

Answers (1)

akrun
akrun

Reputation: 887831

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

Upvotes: 2

Related Questions