Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thisisnic/d976ae5c0bee1a401569380ec9bc9701 to your computer and use it in GitHub Desktop.
Save thisisnic/d976ae5c0bee1a401569380ec9bc9701 to your computer and use it in GitHub Desktop.
If you want to apply the same filter to multiple columns, you can use filter_all() and filter_if() with all_vars() & any_vars().

Code:

library(dplyr)
filter_if(iris, is.numeric, all_vars(.>2.4))

Output:

  Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
1          6.3         3.3          6.0         2.5 virginica
2          7.2         3.6          6.1         2.5 virginica
3          6.7         3.3          5.7         2.5 virginica

Code:

filter_all(mtcars, any_vars(.==15.5))

Output:

   mpg cyl disp  hp drat   wt  qsec vs am gear carb
1 15.5   8  318 150 2.76 3.52 16.87  0  0    3    2
2 19.7   6  145 175 3.62 2.77 15.50  0  1    5    6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment