使用dplyr :: filter的tidyeval方法是什么? [英] What is the tidyeval way of using dplyr::filter?
本文介绍了使用dplyr :: filter的tidyeval方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用foo(c("b"))
调用以下函数.输出显示为内联.
Call the function below using foo(c("b"))
. The outputs are shown inline.
写df %>% filter(!!x > (!!x))
的正确方法是什么?
What is the right way of writing df %>% filter(!!x > (!!x))
?
我提供了一个示例,该示例在tidyeval样式中使用mutate
与filter
进行对比.
I have included an example of using mutate
in tidyeval style to contrast it with filter
.
foo <- function(variables) {
x <- rlang::sym(variables[[1]])
print(x)
#> b
print(typeof(x))
#> [1] "symbol"
df <- data_frame(a = 1, b = 2)
print(df %>% mutate(!!x := 100 + !!x))
#> # A tibble: 1 x 2
#> a b
#> <dbl> <dbl>
#> 1 1 102
print(df %>% filter(!!x > (!!x)))
#> Error in !x : invalid argument type
print(df %>% filter(magrittr::is_greater_than(!!x, !!x)))
#> # A tibble: 0 x 2
#> # ... with 2 variables: a <dbl>, b <dbl>
}
推荐答案
除了打字错误外,您大部分都在那儿,filter语句中的圆括号应该在变量上,而不是值上.
You are most of the way there except for a minor typo, the round brackets in your filter statement should be on the variable and not the value.
print(df %>% filter((!!x) > !!x))
#> # A tibble: 0 x 2
#> # ... with 2 variables: a <dbl>, b <dbl>
这篇关于使用dplyr :: filter的tidyeval方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文