也许我缺少了一些东西,但是我似乎无法使dplyr的取消引用运算符与filter函数一起使用。它与select一起执行,但与filter无关。

例子

  set.seed(1234)
  A = matrix(rnorm(100),nrow = 10, ncol = 10)
  colnames(A) <- paste("var", seq(1:10), sep = "")
  varname_test <- "var2"

  A <- as_tibble(A)

  select(A, !!varname_test) #this works as expected

  # this does NOT give me only the rows where var2
  # is positive
  (result1 <- filter(A, !!varname_test > 0))


  # This is how the result 1 should look like
  (result2 <- filter(A, var2 > 0))

  # result1 is not equal to result2

我将不胜感激任何帮助!

最佳答案

我建议以下内容:

library(dplyr)

filter_at(A, vars(starts_with(varname_test)), any_vars(. > 0))

A tibble: 3 x 10
var1   var2   var3   var4   var5   var6     var7   var8   var9  var10
<dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>    <dbl>  <dbl>  <dbl>  <dbl>
1 -2.35  0.0645  0.460 -0.501 -0.281 -1.01  -0.670    0.648 -0.174  1.00
2  0.429 0.959  -0.694 -1.63  -0.994 -0.162 -0.00760  2.07   0.850 -0.496
3 -0.890 2.42   -0.936 -0.466 -0.497 -1.16   0.336   -0.317 -1.19   2.12

关于r - dplyr取消引用不适用于过滤器功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54350264/

10-12 16:40