如何使用dplyr将所选列重命名为新列名作为字符串

nev*_*int 10 r dplyr

我有以下几点:

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"))
Run Code Online (Sandbox Code Playgroud)

这看起来像这样:

> 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
Run Code Online (Sandbox Code Playgroud)

我想要做的是替换Sepal.LengthPetal.Length附加的字符串to_app <- ".xxx"导致:

  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
Run Code Online (Sandbox Code Playgroud)

我尝试了这个错误:

>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="") ="
Run Code Online (Sandbox Code Playgroud)

aos*_*ith 12

您可以使用此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
Run Code Online (Sandbox Code Playgroud)

您还可以使用选择辅助函数来选择重命名变量,例如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
Run Code Online (Sandbox Code Playgroud)

  • dplyr 1.0.0(并使用 stringr::str_c),rename_with( .data = df, .fn = ~ str_c(., to_app), .cols = contains("Length") )。其他 rename_x 函数已被取代。 (2认同)

ren*_*nsa 11

我对派对来说有点迟了,但是在长时间盯着编程小插图后,我在不同的输入和输出变量中 找到了相关的例子.

在我更简单的用例中,我只需要将列重命名为字符串的值:

> 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
Run Code Online (Sandbox Code Playgroud)

因此,如果您愿意手动执行此操作,您可以:

df %>%
  rename(!!paste("Sepal.Length", "xxx", sep = ".") := Sepal.Length)
Run Code Online (Sandbox Code Playgroud)

但是,如果您需要自动附加".xxx"提供给它的任何列名称的内容,我建议您仔细查看该部分.不幸的是,它仍然有点过头,但我可以看到它是可行的> _>


tal*_*lat 9

如果要使用dplyr的rename函数,最好创建一个命名向量/列表,并使用.dots标准评估版本中的参数调用它:

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
Run Code Online (Sandbox Code Playgroud)

但是请注意,这种方法可能会随着dplyr的下一个版本0.6.0而发生变化(例如http://blog.rstudio.org/2017/04/13/dplyr-0-6-0-coming-soon/http://dplyr.tidyverse.org/articles/programming.html).