Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing filters for dplyr `filter()` in a variable

Tags:

r

dplyr

I want to filter multiple dataframes on the same set of conditions. So I'd like to set those filters as a variable outside the filter() call.

For example

mtcars %>%
  filter(cyl > 4, disp > 100)

I tried doing:

filters <- c(cyl > 4, disp > 100)
mtcars %>%
  filter(filters)

But this doesn't work, because when I set the filters variable, it looks for the dataframe column.

> filters <- c(cyl > 4, disp > 100)
Error: object 'cyl' not found

What is the best way to achieve this?

like image 516
Alex Avatar asked May 03 '18 16:05

Alex


People also ask

How do I filter more than one variable in R?

In this, first, pass your dataframe object to the filter function, then in the condition parameter write the column name in which you want to filter multiple values then put the %in% operator, and then pass a vector containing all the string values which you want in the result.

How does dplyr filter work?

The filter() function is used to subset a data frame, retaining all rows that satisfy your conditions. To be retained, the row must produce a value of TRUE for all conditions. Note that when a condition evaluates to NA the row will be dropped, unlike base subsetting with [ .

Can you filter data in R?

With the crunch package, you can both filter the views of data you work with in your R session and manage the filters that you and your collaborators see in the web application.


1 Answers

The rlang package allows you to create unevaluated expressions that can then be swapped in using special !! notation. If you're using dplyr, you already have the key parts of rlang loaded. Note that for filter, it is good practice to be explicit about combining multiple conditions, rather than relying on filter's implicit "anding" of multiple arguments.

my.filter <- quo(cyl > 4 & disp > 100)

filter(mtcars, !!my.filter)

    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.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
5  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
6  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
7  19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
8  17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
9  16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3

As Artem pointed out, you can, however, use the comma notation with the plural quos function and the !!! operator:

my.filter <- quos(cyl > 4, disp > 100)

filter(mtcars, !!!my.filter)
like image 118
jdobres Avatar answered Sep 24 '22 23:09

jdobres