How to rename selected columns using dplyr with new column names as strings

You can use the rename_at() function for this (starting in dplyr_0.7.0) or rename_with() (starting in dplyr_1.0.0).

For example, you can pass the variables you want to rename as strings. In your example, the paste0 function can be used to append on the appropriate suffix to each column.

cols = c("Sepal.Length", "Petal.Length")
to_app = ".xxx"

For dplyr_1.0.0, use rename_with(). The function argument comes before the column argument. You can use the tidy-select function all_of() (or others) to choose columns.

rename_with(df, .fn = ~paste0(., to_app), .cols = all_of(cols) )

# A tibble: 5 x 3
  Sepal.Length.xxx Sepal.Width Petal.Length.xxx
*            <dbl>       <dbl>            <dbl>
1              5.1         3.5              1.4
2              4.9         3                1.4
3              4.7         3.2              1.3
4              4.6         3.1              1.5
5              5           3.6              1.4

Earlier versions of dplyr use rename_at(). This has been superseded in version 1.0.0, which means there are new functions to use as above but this particular function is not going away.

rename_at(df, cols, list( ~paste0(., to_app) ) )

# A tibble: 5 x 3
  Sepal.Length.xxx Sepal.Width Petal.Length.xxx
*            <dbl>       <dbl>            <dbl>
1              5.1         3.5              1.4
2              4.9         3.0              1.4
3              4.7         3.2              1.3
4              4.6         3.1              1.5
5              5.0         3.6              1.4

You can also use the select helper functions to choose variables for renaming, such as contains.

rename_at(df, vars( contains("Length") ), list( ~paste0(., ".xxx") ) )

# A tibble: 5 x 3
  Sepal.Length.xxx Sepal.Width Petal.Length.xxx
*            <dbl>       <dbl>            <dbl>
1              5.1         3.5              1.4
2              4.9         3.0              1.4
3              4.7         3.2              1.3
4              4.6         3.1              1.5
5              5.0         3.6              1.4

This list() coding replaces the previous funs() coding starting in dplyr_0.7.0. Previously, e.g., funs( paste0(., to_app) )


EDIT: these days, I'd recommend using dplyr::rename_with, as per @aosmith's answer. Write a function that takes your old column names as input and returns your new column names as output, and you're done :)

I'm a little late to the party on this, but after staring at the programming vignette for a long time, I found the relevant example in the Different input and output variable .

In my simpler use case, I just needed to rename a column to the value of a string:

> df1 = data_frame(index = 1:5, value = c(10, 20, 30, 40, 50))
> df1
# A tibble: 5 x 2
  index value
  <int> <dbl>
1     1    10
2     2    20
3     3    30
4     4    40
5     5    50

> newname = 'blau'
> newname2 = 'wheee'

> df1 %>% rename(!!newname := value, !!newname2 := index)
# A tibble: 5 x 2
  wheee  blau
  <int> <dbl>
1     1    10
2     2    20
3     3    30
4     4    40
5     5    50

So if you're happy to do that manually, you can just:

df %>%
  rename(!!paste("Sepal.Length", "xxx", sep = ".") := Sepal.Length)

If, however, you need something that automatically appends ".xxx" to whatever column name is supplied to it, I recommend you have a close look at that section. It's still a bit over my head, unfortunately, but I can see that it's doable >_>


df %>% setNames(paste0(names(.), to.app))

# A tibble: 5 × 3
  Sepal.Length.xxx Sepal.Width.xxx Petal.Length.xxx
*            <dbl>           <dbl>            <dbl>
1              5.1             3.5              1.4
2              4.9             3.0              1.4
3              4.7             3.2              1.3
4              4.6             3.1              1.5
5              5.0             3.6              1.4

EDIT:

Apologies for not reading properly. Here's a solution with data.table package.

var <- names(df)[c(1,3)]
df %>% setnames(., var, paste0(var, to.app))
df

# A tibble: 5 × 3
  Sepal.Length.xxx Sepal.Width Petal.Length.xxx
*            <dbl>       <dbl>            <dbl>
1              5.1         3.5              1.4
2              4.9         3.0              1.4
3              4.7         3.2              1.3
4              4.6         3.1              1.5
5              5.0         3.6              1.4

If you want to use dplyr's rename function, it would be best to create a named vector / list and call it using the .dots argument in the standard evaluation version:

cols <- c("Sepal.Length", "Petal.Length")
to_app <- ".xxx"
cols <- setNames(cols, paste0(cols, to_app))

df %>% rename_(.dots = cols)

## A tibble: 5 × 3
#  Sepal.Length.xxx Sepal.Width Petal.Length.xxx
#*            <dbl>       <dbl>            <dbl>
#1              5.1         3.5              1.4
#2              4.9         3.0              1.4
#3              4.7         3.2              1.3
#4              4.6         3.1              1.5
#5              5.0         3.6              1.4

Note however, that this approach is subject to change with the next version 0.6.0 of dplyr (see e.g. http://blog.rstudio.org/2017/04/13/dplyr-0-6-0-coming-soon/ and http://dplyr.tidyverse.org/articles/programming.html).

Tags:

R

Dplyr