dplyr取消引用不适用于过滤器功能

时间:2019-01-24 15:37:22

标签: r dplyr tibble

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

示例

path = projA/lit

我将不胜感激!

2 个答案:

答案 0 :(得分:3)

我建议以下内容:

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 

答案 1 :(得分:2)

在这里,我的解决方案(1g)使用filter_和使用粘贴建立的条件。 当然,1a是一个很好的解决方案(由joran和aosmith在评论中提供)。

我认为这可能是使用卷曲卷曲的好地方,但我无法使其正常工作(也许不适用?)

我还想:如果我们想按多个变量过滤怎么办?这是您看到2g在下面工作的地方(而2a不再工作)。

其他问题:filter_现在已被弃用,我不确定此处的正确语法是什么。将在一个问题中问这个。

library(tidyverse)
set.seed(1234)
A <- matrix(rnorm(30),nrow = 10, ncol = 3) %>% as_tibble() %>% set_names(paste("var", seq(1:3), sep = ""))
varnames_1 <- c("var2")

(expected_result_1 <- filter(A, var2 > 0))
#> # A tibble: 3 x 3
#>     var1   var2   var3
#>    <dbl>  <dbl>  <dbl>
#> 1 -2.35  0.0645  0.460
#> 2  0.429 0.959  -0.694
#> 3 -0.890 2.42   -0.936

(answer_1a <- filter(A,!!ensym(varnames_1) > 0)) # works (thanks joran and aosmith)
#> # A tibble: 3 x 3
#>     var1   var2   var3
#>    <dbl>  <dbl>  <dbl>
#> 1 -2.35  0.0645  0.460
#> 2  0.429 0.959  -0.694
#> 3 -0.890 2.42   -0.936

(answer_1b <- filter_(A, varnames_1 > 0)) # filter_ not doing what I thought it might
#> Warning: filter_() is deprecated. 
#> Please use filter() instead
#> 
#> The 'programming' vignette or the tidyeval book can help you
#> to program with filter() : https://tidyeval.tidyverse.org
#> This warning is displayed once per session.
#> # A tibble: 10 x 3
#>      var1    var2    var3
#>     <dbl>   <dbl>   <dbl>
#>  1 -1.21  -0.477   0.134 
#>  2  0.277 -0.998  -0.491 
#>  3  1.08  -0.776  -0.441 
#>  4 -2.35   0.0645  0.460 
#>  5  0.429  0.959  -0.694 
#>  6  0.506 -0.110  -1.45  
#>  7 -0.575 -0.511   0.575 
#>  8 -0.547 -0.911  -1.02  
#>  9 -0.564 -0.837  -0.0151
#> 10 -0.890  2.42   -0.936

(answer_1c <- filter(A, {{varnames_1}} > 0)) # curly curly not doing what I thought it might
#> # A tibble: 10 x 3
#>      var1    var2    var3
#>     <dbl>   <dbl>   <dbl>
#>  1 -1.21  -0.477   0.134 
#>  2  0.277 -0.998  -0.491 
#>  3  1.08  -0.776  -0.441 
#>  4 -2.35   0.0645  0.460 
#>  5  0.429  0.959  -0.694 
#>  6  0.506 -0.110  -1.45  
#>  7 -0.575 -0.511   0.575 
#>  8 -0.547 -0.911  -1.02  
#>  9 -0.564 -0.837  -0.0151
#> 10 -0.890  2.42   -0.936
(answer_1d <- filter(A, {{varnames_1 > 0}})) # curly curly not doing what I thought it might
#> `arg` must be a symbol

conditions_1 <- paste(varnames_1, "> 0")
(answer_1e <- filter(A, conditions_1)) # does not work
#> Error: Argument 2 filter condition does not evaluate to a logical vector
(answer_1f <- filter(A, {{conditions_1}})) # curly curly not doing what I thought it might
#> Error: Argument 2 filter condition does not evaluate to a logical vector
(answer_1g <- filter_(A, conditions_1)) # works
#> # A tibble: 3 x 3
#>     var1   var2   var3
#>    <dbl>  <dbl>  <dbl>
#> 1 -2.35  0.0645  0.460
#> 2  0.429 0.959  -0.694
#> 3 -0.890 2.42   -0.936

# what if we wanted to filter multiple variables?

varnames_2 <- c("var2", "var3")
(expected_result_2 <- filter(A, var2 > 0 & var3 > 0))
#> # A tibble: 1 x 3
#>    var1   var2  var3
#>   <dbl>  <dbl> <dbl>
#> 1 -2.35 0.0645 0.460

(answer_2a <- filter(A,!!ensym(varnames_2) > 0)) # does not work
#> Only strings can be converted to symbols

conditions_2 <- paste(paste(varnames_2, "> 0"), collapse = " & ")
(answer_2f <- filter(A, {{conditions_2}})) # curly curly not doing what I thought it might
#> Error: Argument 2 filter condition does not evaluate to a logical vector
(answer_2g <- filter_(A, conditions_2)) # works
#> # A tibble: 1 x 3
#>    var1   var2  var3
#>   <dbl>  <dbl> <dbl>
#> 1 -2.35 0.0645 0.460

reprex package(v0.3.0)于2019-08-28创建