Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore case when using str_detect?

Tags:

r

stringr

stringr package provides good string functions.

To search for a string (ignoring case)

one could use

stringr::str_detect('TOYOTA subaru',ignore.case('toyota')) 

This works but gives warning

Please use (fixed|coll|regex)(x, ignore_case = TRUE) instead of ignore.case(x)

What is the right way of rewriting it?

like image 295
userJT Avatar asked Jun 13 '17 19:06

userJT


2 Answers

You can use regex (or fixed as suggested in @lmo's comment depending on what you need) function to make the pattern as detailed in ?modifiers or ?str_detect (see the instruction for pattern parameter):

library(stringr) str_detect('TOYOTA subaru', regex('toyota', ignore_case = T)) # [1] TRUE 
like image 176
Psidom Avatar answered Sep 20 '22 05:09

Psidom


the search string must be inside function fixed and that function has valid parameter ignore_case

str_detect('TOYOTA subaru', fixed('toyota', ignore_case=TRUE)) 
like image 31
userJT Avatar answered Sep 22 '22 05:09

userJT