Rename all column names with a suffix except listed column name using dplyr?

You can use rename_at and exclude columns using vars helper method:

df <- data.frame(Name = c('a', 'b'), State = c('c', 'd'), col1 = 1:2, col2 = 3:4)
df
#  Name State col1 col2
#1    a     c    1    3
#2    b     d    2    4

Exclude with hard coded names:

df %>% rename_at(vars(-Name, -State), ~ paste0(., '_2017'))
#  Name State col1_2017 col2_2017
#1    a     c         1         3
#2    b     d         2         4

Exclude by column positions:

df %>% rename_at(vars(-(1:2)), ~ paste0(., '_2017'))
#  Name State col1_2017 col2_2017
#1    a     c         1         3
#2    b     d         2         4

Exclude by column names stored in a variable:

to_exclude = c('Name', 'State')
df %>% rename_at(vars(-one_of(to_exclude)), ~ paste0(., '_2017'))
#  Name State col1_2017 col2_2017
#1    a     c         1         3
#2    b     d         2         4

Tags:

R

Dplyr