我想寫一個函數,使用dplyr來計算所有z的唯一值。當我有名爲z的變量時,我的函數可以正常工作。但是,如果變量名爲x,則會出現錯誤(代碼如下)。可以得到非標準的評估工作在dplyr filter_和count_但不distinct__
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)使得它的工作,但我不」不想重新命名一切,尤其是考慮到不同名稱的作品。
我試過lazyeval :: interp和報價()每vignette,this question和this question。
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作爲列名?
嗨。我注意到你的答案在我的答案中給出了8個額外的'test.data',而我的解答給出了6個。我錯過了什麼嗎? – mt1022
@ mt1022是的。我只注意到'filter_'函數工作不正常,'paste()'方法就是要走的路。感謝您的通知! – Psidom