You can use filter_all
in combination with all_vars
from dplyr
, as follows:
some_data <- tibble(var1 = c("a", "b", "c"),
var2 = c(2, 4, 1),
var3 = c(1, 6, 5))
# # A tibble: 3 x 3
# var1 var2 var3
# <chr> <dbl> <dbl>
# 1 a 2.00 1.00
# 2 b 4.00 6.00
# 3 c 1.00 5.00
some_data %>% filter_all(all_vars(. != 1))
# # A tibble: 1 x 3
# var1 var2 var3
# <chr> <dbl> <dbl>
# 1 b 4.00 6.00
This will remove rows in which a variable includes 1. In the above example, this removes the first and third rows. However, be cautious with NA values:
some_data <- tibble(var1 = c("a", "b", "c"),
var2 = c(2, NA, 1),
var3 = c(1, 6, 5))
# # A tibble: 3 x 3
# var1 var2 var3
# <chr> <dbl> <dbl>
# 1 a 2.00 1.00
# 2 b NA 6.00
# 3 c 1.00 5.00
some_data %>% filter_all(all_vars(. != 1))
# # A tibble: 0 x 3
# # ... with 3 variables: var1 <chr>, var2 <dbl>, var3 <dbl>
Note that the second row does not contain a 1, but is filtered anyway. In this specific example, you can avoid such behavior by:
some_data %>% filter_all(all_vars(. != 1 | is.na(.)))
However, this may not generalize well.