1

I would like to be able to filter(), where the argument values are variables that have already been defined.

So for example, using the reproducible mtcars dataset:

library(tidyverse)
df <- mtcars
head(df)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

I'd like to use:

filter_by <-  "cyl"
filter_equal <- 6

I tried the below, but I'm getting zero rows for output.

df %>% filter(filter_by == filter_equal)
#>  [1] mpg  cyl  disp hp   drat wt   qsec vs   am   gear carb
#> <0 rows> (or 0-length row.names)

My desired output is something like this (but using the variables filter_by and filter_equal):

df %>% filter(cyl == 6)
#>    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> 2 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> 3 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> 4 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> 5 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> 6 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> 7 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6

Created on 2019-08-25 by the reprex package (v0.2.1)

Jeremy K.
  • 1,710
  • 14
  • 35

2 Answers2

7

If you want to use non-standard evaluation (NSE) you need to convert filter_by to symbol (sym) and then evaluate (!!)

library(dplyr)
library(rlang)

df %>% filter(!!sym(filter_by) == filter_equal)

#   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#1 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#2 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#3 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#4 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#5 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#6 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#7 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6

We can also use get

df %>% filter(get(filter_by) == filter_equal)

Without NSE in base R, we can directly do

df[df[[filter_by]] == filter_equal, ]
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • This solved my issue of dbplyr not handling strings as variable arguments (unlike dplyr, which does) – Matt Jan 05 '23 at 18:02
1

We can use filter_at

library(dplyr)
df %>%
   filter_at(vars(filter_by), any_vars(. == filter_equal))
#   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#1 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#2 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#3 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#4 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#5 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#6 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#7 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
akrun
  • 874,273
  • 37
  • 540
  • 662