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

Iman

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

akrun

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

dplyr: access column name in mutate_at function

mutate_ column using a calculated column name in dplyr

Changing a column's name to titlecase using dplyr, by means of a function

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

Dplyr mutate minimum column name

Convert a character to a column name when using "dplyr::mutate"

Mutate with a list column function in dplyr

dplyr's mutate at each column separately with a custom function of several parametrs

dplyr - mutate using function that uses other column data as argument?

Using custom mutate function in dplyr

How to find the sum of two consecutive rows of a column from a dataframe based on a condition of another columns(using,maybe, dplyr's mutate function)

dplyr: how to reference columns by column index rather than column name using mutate?

dplyr mutate, custom function and variable name as characters

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's mutate function: add column with value, based on filtration of another column

dplyr mutate using conditional column and specific rows

In R dplyr, gsub() in mutate() using column as the pattern

creating a column with replaced values using mutate and dplyr

dplyr 0.5.0 mutate using column index

dplyr: using column created by mutate in the mutation itself

exp function in dplyr mutate returning 1's

Using a custom function inside of dplyr mutate?

Using dplyr mutate_at with custom function

Using dplyr quosure custom function with mutate_at

R, pass column name as argument to function using dplyr::filter() and %in%

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

Why can't I apply a function to create a new column with mutate() using dplyr?