How to specify “does not contain” in dplyr filter

前端 未结 2 1925
予麋鹿
予麋鹿 2020-12-08 02:27

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

相关标签:
2条回答
  • 2020-12-08 02:49

    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)
    
    0 讨论(0)
  • 2020-12-08 02:59

    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 -.

    0 讨论(0)
提交回复
热议问题