How to specify "does not contain" in dplyr filter

RFilterDplyr

R Problem Overview


I am quite new to R.

Using the table called SE_CSVLinelist_clean, I want to extract the rows where the Variable called where_case_travelled_1 DOES NOT contain the strings "Outside Canada" OR "Outside province/territory of residence but within Canada". Then create a new table called SE_CSVLinelist_filtered.

SE_CSVLinelist_filtered <- filter(SE_CSVLinelist_clean, 
where_case_travelled_1 %in% -c('Outside Canada','Outside province/territory of residence but within Canada'))

The code above works when I just use "c" and not "-c".
So, how do I specify the above when I really want to exclude rows that contains that outside of the country or province?

R Solutions


Solution 1 - R

Note that %in% returns a logical vector of TRUE and FALSE. To negate it, you can use ! in front of the logical statement:

SE_CSVLinelist_filtered <- filter(SE_CSVLinelist_clean, 
 !where_case_travelled_1 %in% 
   c('Outside Canada','Outside province/territory of residence but within Canada'))

Regarding your original approach with -c(...), - is a unary operator that "performs arithmetic on numeric or complex vectors (or objects which can be coerced to them)" (from help("-")). Since you are dealing with a character vector that cannot be coerced to numeric or complex, you cannot use -.

Solution 2 - R

Try putting the search condition in a bracket, as shown below. This returns the result of the conditional query inside the bracket. Then test its result to determine if it is negative (i.e. it does not belong to any of the options in the vector), by setting it to FALSE.

SE_CSVLinelist_filtered <- filter(SE_CSVLinelist_clean, 
(where_case_travelled_1 %in% c('Outside Canada','Outside province/territory of residence but within Canada')) == FALSE)

Solution 3 - R

Just be careful with the previous solutions since they require to type out EXACTLY the string you are trying to detect.

Ask yourself if the word "Outside", for example, is sufficient. If so, then:

data_filtered <- data %>% 
  filter(!str_detect(where_case_travelled_1, "Outside")

A reprex version:

iris

iris %>% 
  filter(!str_detect(Species, "versicolor"))

Solution 4 - R

Quick fix. First define the opposite of %in%:

  '%ni%' <- Negate("%in%")

Then apply:

SE_CSVLinelist_filtered <- filter(
    SE_CSVLinelist_clean, 
    where_case_travelled_1 %ni% c('Outside Canada',
      'Outside province/territory of residence but within Canada'))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaykView Question on Stackoverflow
Solution 1 - RfishtankView Answer on Stackoverflow
Solution 2 - RBWOView Answer on Stackoverflow
Solution 3 - RAustinView Answer on Stackoverflow
Solution 4 - RToWiiView Answer on Stackoverflow