【发布时间】:2016-12-16 17:09:21
【问题描述】:
我正在尝试编写一个使用 dplyr 来计算 z 的所有唯一值的函数。当我将变量实际命名为 z 时,我的函数工作正常。但是,如果变量名为 x,我会收到错误消息(如下代码)。
test.data<-data.frame(y=c(1:10),
x=c(letters[1:10]))
test.data$x<-as.character(test.data$x)
obsfunction<-function(z,y,data){
filter_(data,
!is.na(deparse(substitute(y))))%>%
distinct_(., deparse(substitute(z)))%>% #the line that breaks it
count_(.)
}
obsfunction(z=x,y,data=test.data)
所以,上面的代码不起作用并给出了这个错误:
>Error in eval(substitute(expr), envir, enclos) : unknown column 'z'
在函数中将 z 更改为 x(或将 x 重命名为 z)使其工作,但我不想重命名所有内容,特别是考虑到 y 使用不同的名称。
我已经按照vignette、this question 和this question 尝试了lazyeval::interp 和quote()。
distinct_(lazyeval::interp(as.name(z)))%>%
>Error in as.name(z) : object 'x' not found
distinct_(quote(z))%>%
>Error in eval(substitute(expr), envir, enclos) : unknown column 'z'
我错过了什么?如何让 z 接受 x 作为列名?
【问题讨论】: