gpt4 book ai didi

r - 可以获得非标准评估以在 dplyr 中为 filter_ 和 count_ 但不是 distinct_ 工作

转载 作者:行者123 更新时间:2023-12-04 10:45:27 25 4
gpt4 key购买 nike

我正在尝试编写一个使用 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 尝试过 lazyeval::interp 和 quote() , 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 作为列名?

最佳答案

作为 dplyr 标准评估理解字符串,我尝试了以下代码和额外的测试数据,它似乎有效。我首先提取变量名,然后使用字符串构造表达式:

test.data<-data.frame(y=c(1:10),
x=c(letters[1:10]))
test.data$x<-as.character(test.data$x)

f <- function(z, y, data){
z <- deparse(substitute(z))
y <- deparse(substitute(y))
res <- data %>% filter_(
paste('!is.na(', y, ')', sep = '')) %>%
distinct_(z) %>%
count_(.)
}


x <- f(z = x, y, test.data)
# # A tibble: 1 × 1
# n
# <int>
# 1 10



test.data <- data.frame(
y=c(1:4, NA, NA, 7:10),
x=c(letters[c(1:8, 8, 8)]),
stringsAsFactors = F)

x <- f(z = x, y, test.data)
# # A tibble: 1 × 1
# n
# <int>
# 1 6

关于r - 可以获得非标准评估以在 dplyr 中为 filter_ 和 count_ 但不是 distinct_ 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41189234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com