1

I need to exclude any instance of six values from a dataset that I have and wonder if there is an "all-in-one" solution to achieve this.

Aside from the usual

df[df$val != "one" || df$val != "two", ]

approach, is there an easier way to achieve this when there are a larger number of criteria?

I have tried

combi %>%
  filter(siteLocation != c("", "VC2", "LB", "GHNA", "GH", "GA"))

and

combi %>%
  filter(siteLocation != "" | "VC2" | "LB" | "GHNA" | "GH" | "GA")

but neither work (you get the idea).

Is this possible and if so, how?

Mus
  • 7,290
  • 24
  • 86
  • 130

1 Answers1

-1

We can use %in% with ! i.e. create a logical vector on a vector of elements on the rhs of %in%, and negate (!)

library(dplyr)
combi %>%
   filter(! siteLocation %in% c("", "VC2", "LB", "GHNA", "GH", "GA"))

NOTE: Using != or == results in incorrect output as these are elementwise comparisons i.e it checks the first element of 'siteLocation' not equal to "", then 2nd element with 'VC2' and so on until it reaches the last element of the vector. Then, it does a recycling starting from ""...

akrun
  • 874,273
  • 37
  • 540
  • 662