I have below code
library(dplyr)
New_Col_Name = 'Some_Name'
dat = data.frame('a' = 3)
dat %>% rename(a = New_Col_Name)
Basically, I am trying to rename with a dynamic variable New_Col_Name
.
ABove code generates an error. Could you please help to correctly write above code?
rename()
expects its arguments the other way round, exactly like regular assignment.
And if you want to use a dynamic name, you cannot just say rename(new = old)
, since that will attempt to use the variable name literally (i.e. it will create a column called new
). To use a computed/dynamic name, you need to use tidy evaluation with the :=
operator and interpolation:
dat %>% rename(!! New_Col_Name := a)