Pass a variable to dplyr "rename" to change columnname

dplyr
#1

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)

2 Likes

#2

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?

5 Likes

#3

Hi Rensa,

Worked like a charm! Thank you very much.

Greetings

1 Like

#4

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

0 Likes