Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr unquoting does not work with filter function

Tags:

r

dplyr

tibble

maybe I am missing something, but I can't seem to make dplyr's unquoting operator to work with the filter function. It does with with select, but not with filter...

Example

  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

I would appreciate any help!

like image 911
Jean_N Avatar asked Jan 24 '19 15:01

Jean_N


2 Answers

I would suggest the following:

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 
like image 62
Florian Avatar answered Sep 24 '22 13:09

Florian


Here, my solution (1g) uses filter_ and conditions built up with paste. Of course, 1a is a perfectly fine solution (as was provided by joran and aosmith in the comments).

I thought this might be a good place to use curly curly but I couldn't get it to work (maybe not applicable?)

I also thought: what if we wanted to filter by multiple variables? This is where you see 2g working below (while 2a does not work anymore).

Other issues: filter_ is now deprecated, and I'm not sure what the correct syntax would be here. Will be asking this in a question.

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

Created on 2019-08-28 by the reprex package (v0.3.0)

like image 32
Arthur Yip Avatar answered Sep 25 '22 13:09

Arthur Yip