dplyr'rename'标准评估功能不能按预期工作? [英] dplyr 'rename' standard evaluation function not working as expected?

查看:121
本文介绍了dplyr'rename'标准评估功能不能按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:从这个帖子下面的评论来看,这是正常工作,没有我在这里提出的问题。

UPDATE: From comment below on this post, this is now working as expected, without the issues I laid out here.

下面是一个使用重命名_ 从dplyr。我期望能够使用下面的第二个例子将列名改回它的原始名称,但我猜测函数参数评估规则以某种方式阻止它按照我的想法运行。有一个简单的解决方法,使用原始的plyr包 rename 函数(以及使用基本包名称函数)但我有一种感觉,我错过了一个dplyr的解决方案。

Below is a toy example of using rename_ from dplyr. I was expecting to be able to change the column name back to it's original name using the second example below, but I'm guessing that function argument evaluation rules are somehow preventing it from working the way I think. There is an easy workaround using the original plyr package rename function (as well as using the base package names function), but I have a feeling I'm missing a dplyr solution to this.

我有一个解决方法如下所示,但我欢迎两个dplyr解决方案到第二个例如按照我的预期工作,或者说我为什么不要指望它按照我想要的方式工作。

I have a workaround as shown below, but I'd welcome both a dplyr solution to the second example working as I expect, or an explanation of why I shouldn't expect it to work the way I want it to.

谢谢
Matt

Thank you, Matt

编辑:我在下面添加了一个例子,使用 rename _ 来使这项工作变得复杂。我假设如果Hadley所提到的错误被修正了,这将在下面显示。但直到那时,我的尴尬的方式,但最好使用标准的 plyr 方法。还添加了基础R技术,例如完整性。

I added an example below using rename_ to make this work, but is complicated. I assume if the bug that Hadley refers to below gets fixed, this will work as he shows below. But until then, my awkward way does, but it is probably better to use the standard plyr method. Also added base R technique at end for example completeness.

library(plyr)
library(dplyr)

# dataframe to operate on
dat <- data_frame(a=1, b=1)

# identifier with string of column name in dat
x <- "a"


# Renaming using standard evaluation this way works
dat %>%
    rename_("new" = x)
# Source: local data frame [1 x 2]
# 
#   new b
# 1   1 1


# But changing it back does not
# I expect "a" to be the name, not the identifier x
dat %>%
    rename_("new" = x) %>%
    rename_(x = "new")
# Source: local data frame [1 x 2]
# 
#   x b
# 1 1 1


# This works, but seems really awkward...
dat %>%
    rename_("newname" = x) %>%
    do(do.call(rename_, setNames(list(., "newname"), c(".data", x))))

# Source: local data frame [1 x 2]
# 
#   a b
# 1 1 1


# This works fine
dat %>%
    rename_("new" = x) %>%
    plyr::rename(c("new" = x))
# Source: local data frame [1 x 2]
# 
#   a b
# 1 1 1


# Base R way
datrn <- dat %>%
    rename_("newname" = x)
names(datrn)[names(datrn) == "newname"] = x
datrn
# Source: local data frame [1 x 2]
# 
#   a b
# 1 1 1


推荐答案

有一些事情让人痛心:


  1. c(x =new) c(x=new ),而不是相反的
    $ c(new = x)。

您可以使用 setNames(x,new)
或...

You can construct the vector you want with setNames(x, "new"), but...

我忘了将 .dots 参数添加到 rename _ (bug报告在
https://github.com/hadley/dpl yr / issues / 708 ),所以你不能做:

I forgot to add the .dots argument to rename_ (bug report at https://github.com/hadley/dplyr/issues/708) so you can't do:

rename_(dat, .dots = setNames(x, "new"))

相反,您需要使用 do.call

do.call(rename_, c(list(quote(dat)), list(setNames(x, "new"))))


这篇关于dplyr'rename'标准评估功能不能按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆