In case anyone is looking for a very scalable solution that is applicable if you want to test multiple columns for the same condition, you can use Reduce
or rowSums
.
Sample Data
df <- base::expand.grid(x = c(0, 1),
y = c(0, 1),
z = c(0, 1))
df
#> x y z
#> 1 0 0 0
#> 2 1 0 0
#> 3 0 1 0
#> 4 1 1 0
#> 5 0 0 1
#> 6 1 0 1
#> 7 0 1 1
#> 8 1 1 1
Solution
Does it contain any 0? Keeps every row except for row 8 that is filled with 1 only.
The function + in Reduce()
basically works as an OR operator since its result is >0 if it contains any TRUE value.
## Reduce ---------------------------------------------------
df[Reduce(f = `+`, x = lapply(df, `==`, 0)) > 0, ]
#> x y z
#> 1 0 0 0
#> 2 1 0 0
#> 3 0 1 0
#> 4 1 1 0
#> 5 0 0 1
#> 6 1 0 1
#> 7 0 1 1
## rowSums --------------------------------------------------
df[rowSums(df == 0) > 0, ]
#> x y z
#> 1 0 0 0
#> 2 1 0 0
#> 3 0 1 0
#> 4 1 1 0
#> 5 0 0 1
#> 6 1 0 1
#> 7 0 1 1
Multiple AND-Conditions
Note that you can use Reduce
also easily to apply multiple AND
conditions by using *
instead of +
. Multiplying all logicals only returns a value >0
if all cases are TRUE
.
df[Reduce(`*`, lapply(df, `==`, 0)) > 0, ]
#> x y z
#> 1 0 0 0