How to properly ignore case in regex

508 Views Asked by At

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.

1

There are 1 best solutions below

0
Ronak Shah On BEST ANSWER

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