Original data frame:
df <- data.frame(col1=c("A", "B"), col2=c("C", "D"), stringsAsFactors=F)
df
col1 col2
1 A C
2 B D
Using dplyr mutate_at, let's say I would like to replace all values of column col2 with the name of the column, in order to get:
df <- data.frame(col1=c("A", "B"), col2=c("col2", "col2"), stringsAsFactors=F)
df
col1 col2
1 A col2
2 B col2
Using the following for instance, triggers an error:
df %>%
dplyr::mutate_at("col2", function(x){x=rlang::as_string(x)})
Note: the point of this example is not to replace all values of column col2, but to know how to convert vars(col2) vector back to a character string "col2".
Similar thread: R dplyr mutate_at accessing colnames