How to rename selected columns with dplyr with new column names as rows

I have the following piece:

library(tidyverse)
df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5), Sepal.Width = c(3.5, 
3, 3.2, 3.1, 3.6), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4)), .Names = c("Sepal.Length", 
"Sepal.Width", "Petal.Length"), row.names = c(NA, 5L), class = c("tbl_df", 
"tbl", "data.frame"))

It looks like this:

> df
# A tibble: 5 × 3
  Sepal.Length Sepal.Width Petal.Length
*        <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

I want to replace Sepal.Lengthand the Petal.Lengthadded line to_app <- ".xxx"will result in:

  Sepal.Length.xxx Sepal.Width Petal.Length.xxx
          5.1         3.5          1.4
          4.9         3.0          1.4
          4.7         3.2          1.3
          4.6         3.1          1.5
          5.0         3.6          1.4

I tried this with an error:

>df %>% rename(paste(Sepal.Length,to_app,sep="") = Petal.Length,paste(Sepal.Width,to_app,sep="") = Petal.Length)
Error: unexpected '=' in "df %>% rename(paste(Sepal.Length,to_app,sep="") ="
+9
source share
6 answers

If you want to use the renamedplyr function , it would be better to create a named vector / list and call it using an argument .dotsin 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

, dplyr 0.6.0 (., , http://blog.rstudio.org/2017/04/13/dplyr-0-6-0-coming-soon/ http://dplyr.tidyverse.org/articles/programming.html).

+9

rename_at ( dplyr_0.7.0).

, , , . paste0 .

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

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

, , 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

list() funs funs() dplyr_0.7.0. , , funs( paste0(., to_app) )

+12

, , .

:

> 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

, , :

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

-, ".xxx" , , . , , , > _ >

+9
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 incorrect reading. Here is the solution with the 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
+3
source

Assuming the goal is to rename all columns containing "Length":

colnames(df) <- ifelse(grepl("Length", colnames(df)), 
                       paste0(colnames(df), to_app), 
                       colnames(df))
+2
source

The best I can do in the development version dplyr(coming out May 11):

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

rename(df, 
       !!ns[1] := !!as.name(cols[1]), 
       !!ns[2] := !!as.name(cols[2]))

To do this fully programmatically, use quos:

xx <- do.call(quos, setNames(map(cols, as.name), ns))
rename(df, !!!xx)

Both give:

# 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

One liner:

rename(df, !!!do.call(quos, setNames(map(cols, as.name), paste0(cols, to_app))))
+1
source

Source: https://habr.com/ru/post/1682901/


All Articles