Pass a variable to dplyr "rename" to change columnname

What I am trying to do is: rename column from a dataframe with a variable. My approch is not working. Any help is appreciated.

example: (is not working)

variable <- sym("new_name")
df %>% rename(!!variable = name_of_col_from_df)

4 Likes

I'm not the best person to explain the ins and outs of tidyeval, but I think that if you're using bang-bang (!!) to unquote the left hand side of an assignment, you need to use a special substitute := operator:

df %>% rename(!!variable := name_of_col_from_df)

How does that go for you?

20 Likes

Hi Rensa,

Worked like a charm! Thank you very much.

Greetings

1 Like

If your question's been answered— even by you, would you mind choosing a solution? (see FAQ below for how) It makes it a bit easier to visually navigate the site and see which questions still need help.

Thanks