【问题标题】:Rename variable names in dplyr based on vectors new_varname, old_varname [duplicate]根据向量new_varname,old_varname重命名dplyr中的变量名[重复]
【发布时间】:2016-10-02 12:54:21
【问题描述】:

是否有一种“dplyr 方式”可以根据变量名称转换器 (vt) 重命名来自 data.frame 的变量子集,该 data.frame 包含具有旧变量名称和新变量名称的列(分别为 old_varname 和 new_varname) . 例如:

d <- iris
vt <- data.frame(old_varname=c('Sepal.Length','Petal.Length'),
                  new_varname=c('a','b'))
d <- d %>% rename_( .... )
#In base R code, this would be:
names(d)[names(d) %in% vt$old_varname] <- vt$std_varname

编辑: 进一步澄清:

  • 假设要翻译的变量向量很长,那么手工编写新旧名称对是不可行的
  • 要重命名的变量是总变量的子集,我还是想保留所有变量

【问题讨论】:

  • 我认为对于这种情况,使用 base-R 解决方案会更好。有什么不适合你的?
  • @Shorpy:问题是我为繁重的数据操作加载了 data.table。 Data.table 改变了子集 "[ ]" 的 sintax 并破坏了一些代码。所以我试图在 dplyr 中包含与元数据相关的代码。

标签: r dplyr rename


【解决方案1】:

卢卡斯,

感谢您的澄清:

您可以使用 data.table::setnames()。希望这会有所帮助。

if (!require(data.table)) install.packages(data.table)
data(iris)
d <- iris
head(d)
old_varname=c('Sepal.Length','Petal.Length')
new_varname=c('a','b')
d2 <- d %>% data.table::setnames(old = old_varname, new = new_varname)
head(d2)

输出:

> if (!require(data.table)) install.packages(data.table)
> data(iris)
> d <- iris
> head(d)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
> old_varname=c('Sepal.Length','Petal.Length')
> new_varname=c('a','b')
> d2 <- d %>% data.table::setnames(old = old_varname, new = new_varname)
> head(d2)
    a Sepal.Width   b Petal.Width Species
1 5.1         3.5 1.4         0.2  setosa
2 4.9         3.0 1.4         0.2  setosa
3 4.7         3.2 1.3         0.2  setosa
4 4.6         3.1 1.5         0.2  setosa
5 5.0         3.6 1.4         0.2  setosa
6 5.4         3.9 1.7         0.4  setosa

【讨论】:

  • 对不起,我的帖子不清楚(我现在会编辑)。假设要翻译的变量向量很长,因此手动编写旧 - 新名称对(如您的答案)是不可行的。谢了。
  • @LucasMation - 在更新的问题上下文中,我的偏好是 data.table::setnames()。查看更新的示例。
【解决方案2】:

使用setNames()

    > df <- letters[1:5]
    > iris %>% setNames(df)

如果您知道要在原始数据中重命名的列的索引,则可以使用 setnames()from data.table 包来实现

   > df <- letters[24:26]
   > df
   [1] "x" "y" "z"
   > setnames(iris,names(iris)[c(1,2,5)],df)
   > head(iris)
       x   y Petal.Length Petal.Width      z
   1 5.1 3.5          1.4         0.2 setosa
   2 4.9 3.0          1.4         0.2 setosa
   3 4.7 3.2          1.3         0.2 setosa
   4 4.6 3.1          1.5         0.2 setosa
   5 5.0 3.6          1.4         0.2 setosa
   6 5.4 3.9          1.7         0.4 setosa

【讨论】:

  • Tks。这似乎是正确的方向。但是,翻译字典中包含的变量只是总变量的一个子集,我想保留它们(见我上面的编辑)
  • 您说的是假设您有 5 列,并且您想重命名其中的任何 3 列,其余两个名称将是原始名称。对吗?
  • 是的!但是你不能假设变量的顺序在数据集和字典中是相同的
【解决方案3】:

试试这个:

d <- iris
vt <- data.frame(old_varname=c('Sepal.Length','Petal.Length'),
              new_varname=c('a','b'), stringsAsFactors = F)
d.out <- d %>% rename_(.dots = setNames(vt$old_varname, vt$new_varname))

head(d.out)
    a Sepal.Width   b Petal.Width Species
1 5.1         3.5 1.4         0.2  setosa
2 4.9         3.0 1.4         0.2  setosa
3 4.7         3.2 1.3         0.2  setosa
4 4.6         3.1 1.5         0.2  setosa
5 5.0         3.6 1.4         0.2  setosa
6 5.4         3.9 1.7         0.4  setosa

请注意setNames的第一个参数不能接受factor,所以我修改vt添加stringsAsFactors = F

【讨论】:

  • 这看起来不错,但我收到一个错误:# Error: All arguments to rename must be named.
  • @LucasMation 嗯,我不确定出了什么问题。但是我更新了答案以显示我这边的完整过程
  • rename_ 现在已弃用。如果您想使用 tidyverse(在这种情况下为 dplyr 和 purrr),Jeff Hammerbacher 对 rename() 和 !!set_names() 的回答在此时确实有效
【解决方案4】:

解决此问题的另一种方法是取消引用命名向量,其中新列名作为名称,旧列名作为值。请注意,我使用purrr::set_names 来制作命名向量。

library(tidyverse)
d <- as_tibble(iris)
vt <- tibble(old_varname=c('Sepal.Length','Petal.Length'),
             new_varname=c('a','b'))
d_new_names <- d %>% rename(!!set_names(vt$old_varname, vt$new_varname))
head(d_new_names)

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 2017-06-10
    • 1970-01-01
    • 2018-11-22
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    相关资源
    最近更新 更多