R: How to ignore case when using str_detect?

ε祈祈猫儿з 提交于 2020-01-01 04:04:55

问题


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?


回答1:


You can use regex (or fix as @lmo's comments 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



回答2:


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



回答3:


You can use the base R function grepl() to accomplish the same thing without a nested function. It simply accepts ignore.case as an argument.

grepl("toyota", 'TOYOTA subaru', ignore.case = TRUE)

(Note that the order of the first two arguments (pattern and string) are switched between grepl and str_detect).



来源:https://stackoverflow.com/questions/44530029/r-how-to-ignore-case-when-using-str-detect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!