Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dplyr

I'm hoping this is a simple question. How do I rename all of the column header names in a data set with the suffix "_2017" except the first 2 column headers which are: "Name", and "State"? I'd like to use dplyr to do this.

like image 250
Jazzmatazz Avatar asked Jun 04 '18 13:06

Jazzmatazz


People also ask

How do I rename a column in R using dplyr?

rename() function from dplyr takes a syntax rename(new_column_name = old_column_name) to change the column from old to a new name. The following example renames the column from id to c1 . The operator – %>% is used to load the renamed column names to the data frame.

How do I remove suffix from column names in R?

To remove all columns with a common suffix from an R data frame, you need to use the grep() function. This function identifies and returns a vector with all columns that share the suffix. You can use this vector as an argument of the select() function to remove the columns from the data frame.

How do I change all column names in R?

Rename All Column Names Using colnames() colnames() is the method available in R which can be used to rename all column names (list with column names). Actually, colnames() is used to set or get the names of a dataframe. You can also use this method to rename dataframe column by index in R.

How do I replace multiple column names in R?

To change multiple column names by name and by index use rename() function of the dplyr package and to rename by just name use setnames() from data. table . From R base functionality, we have colnames() and names() functions that can be used to rename a data frame column by a single index or name.


2 Answers

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
like image 185
Psidom Avatar answered Nov 15 '22 01:11

Psidom


rename_if(), rename_at(), and rename_all() have been superseded by rename_with().

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

# De-select by location
df %>% rename_with(~paste0(., "_2017"), -c(1:2))

#>   Name State col1_2017 col2_2017
#> 1    a     c         1         3
#> 2    b     d         2         4


# De-select by name 
df %>% rename_with(~paste0(., "_2017"), -c("Name", "State"))

#>   Name State col1_2017 col2_2017
#> 1    a     c         1         3
#> 2    b     d         2         4


# De-select using another variable that holds names
deselect_names <- c("Name", "State")
df %>% rename_with(~paste0(., "_2017"), -!!deselect_names)

#>   Name State col1_2017 col2_2017
#> 1    a     c         1         3
#> 2    b     d         2         4
like image 40
HBat Avatar answered Nov 14 '22 23:11

HBat