In dplyr 1.0.0, what is the right way to write a logical disjunction?
We can use filter
with across
with reduce
library(dplyr)
library(purrr)
iris %>%
filter(across(starts_with("sepal"), ~ . > 5) %>% reduce(`|`))
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.1 3.5 1.4 0.2 setosa
#2 5.4 3.9 1.7 0.4 setosa
#3 5.4 3.7 1.5 0.2 setosa
#4 5.8 4.0 1.2 0.2 setosa
#5 5.7 4.4 1.5 0.4 setosa
#6 5.4 3.9 1.3 0.4 setosa
#7 5.1 3.5 1.4 0.3 setosa
# ...
Is this what you're looking for? Here we include any rows where either Sepal.Length or Sepal.Width is greater than 3.
c_across
takes the specified columns and treats each row of those variables as a vector, iterating one row at a time. So, you can perform rowwise filtering by checking if any of the specified columns in the row are greater than 3.
library(dplyr)
iris %>%
rowwise() %>%
filter(any(c_across(starts_with("sepal")) > 5))
#> # A tibble: 118 x 5
#> # Rowwise:
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 5.4 3.9 1.7 0.4 setosa
#> 3 5.4 3.7 1.5 0.2 setosa
#> 4 5.8 4 1.2 0.2 setosa
#> 5 5.7 4.4 1.5 0.4 setosa
#> 6 5.4 3.9 1.3 0.4 setosa
#> 7 5.1 3.5 1.4 0.3 setosa
#> 8 5.7 3.8 1.7 0.3 setosa
#> 9 5.1 3.8 1.5 0.3 setosa
#> 10 5.4 3.4 1.7 0.2 setosa
#> # … with 108 more rows
Created on 2020-07-02 by the reprex package (v0.3.0)