你能帮帮我吗
我已经尝试了很多组合,但没有任何效果。
这里是例子:
library(dplyr)
foo <- function(AA,x){
mn <- make.names(x)
mn <- enquo(mn)
filter(AA,mn==min(!!mn))
}
aa <- data.frame(A=c("a","b","c","d"), B.D = c(1,2,1,3))
foo(aa,"B D")
输出是
Error: Base operators are not defined for quosures.
Do you need to unquote the quosure?
# Bad:
myquosure == rhs
# Good:
!!myquosure == rhs
Run `rlang::last_error()` to see where the error occurred.
代替
filter(aa,B.D==min(B.D))
A B.D
1 a 1
2 c 1
能否请您帮助我使用我的函数获得所需的输出。
谢谢。
约翰
最佳答案
使用 {{ }}
“embrace”或“double curly”运算符稍微简单一些,但它需要变量名称 raw vs. 作为字符串。
foo <- function(AA,x){
filter(AA, {{ x }} == min({{ x }}))
}
foo(aa, B.D) # or foo(aa, `B.D`)
A B.D
1 a 1
2 c 1
关于R dplyr列作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66696047/