Jeff Parker
Jeff Parker

Reputation: 1969

Renaming multiple columns from vector in dplyr chain

I have a dataframe that I would like to rename several columns with similar name conventions (e.g., starts with "X") and/or column positions (e.g., 4:7). The new names of the columns are stored in a vector. How do I rename this columns in a dplyr chain?

# data
df <- tibble(RID = 1,Var1 = "A", Var2 = "B",old_name1 =4, old_name2 = 8, old_name3=20)
new_names <- c("new_name1","new_name2","new_name3")

#psuedo code
df %>%
  rename_if(starts_with('old_name'), new_names)

Upvotes: 4

Views: 7189

Answers (2)

Agile Bean
Agile Bean

Reputation: 7141

Update dplyr 1.0.0

There is an addition to rename() by rename_with() which takes a function as input. This function can be function(x) return (new_names), in other words you use the purrr short form ~ new_names as the rename function.

This makes imho the most elegant dplyr expression.

# shortest & most elegant expression
df %>% rename_with(~ new_names, starts_with('old_name'))

# A tibble: 1 x 6
    RID Var1  Var2  new_name1 new_name2 new_name3
  <dbl> <chr> <chr>     <dbl>     <dbl>     <dbl>
1     1 A     B             4         8        20

Upvotes: 5

akrun
akrun

Reputation: 886938

An option with rename_at would be

df %>% 
  rename_at(vars(starts_with('old_name')), ~ new_names)
# A tibble: 1 x 6
#    RID Var1  Var2  new_name1 new_name2 new_name3
#   <dbl> <chr> <chr>     <dbl>     <dbl>     <dbl>
#1  1.00 A     B          4.00      8.00      20.0

But, it is possible to make a function that works with rename_if by creating a logical index on the column names

df %>%
    rename_if(grepl("^old_name", names(.)), ~ new_names)
# A tibble: 1 x 6
#    RID Var1  Var2  new_name1 new_name2 new_name3
#  <dbl> <chr> <chr>     <dbl>     <dbl>     <dbl>
#1  1.00 A     B          4.00      8.00      20.0

The rename_if in general is checking at the values of the columns instead of the column names i.e.

new_names2 <- c('var1', 'var2')
df %>%
     rename_if(is.character, ~ new_names2)
# A tibble: 1 x 6
#    RID var1  var2  old_name1 old_name2 old_name3
#   <dbl> <chr> <chr>     <dbl>     <dbl>     <dbl>
#1  1.00 A     B          4.00      8.00      20.0

Upvotes: 11

Related Questions