rregexignore-case

How to properly ignore case in regex


This is about R. Can someone look at this:

{library(forcats)
x <- filter(gss_cat, rincome != regex("not applicable", ignore_case = TRUE))}

The ignore_case = TRUE has no effect. "Not applicable" and "not applicable" still look different to the search.


Solution

  • Consider this example :

    df <- data.frame(a = c('This is not applicable', 'But this is applicable', 
                           'This is still NOT aPPLicable'))
    

    You need to use regex in one of the stringr function like str_detect here :

    library(dplyr)
    library(stringr)
    
    df %>% filter(str_detect(a, regex('not applicable', 
                  ignore_case = TRUE), negate = TRUE))
    
    #                      a
    #1 But this is applicable
    

    Or in base R use subset with grepl

    subset(df, !grepl('not applicable', a, ignore.case = TRUE))