Case-insensitive search of a list in R

前端 未结 7 1986
不知归路
不知归路 2020-11-30 04:58

Can I search a character list for a string where I don\'t know how the string is cased? Or more generally, I\'m trying to reference a column in a dataframe, but I don\'t kn

7条回答
  •  鱼传尺愫
    2020-11-30 05:17

    With the stringr package, you can modify the pattern with one of the built in modifier functions (see `?modifiers). For example since we are matching a fixed string (no special regular expression characters) but want to ignore case, we can do

    str_detect(colnames(iris), fixed("species", ignore_case=TRUE))
    

    Or you can use the (?i) case insensitive modifier

    str_detect(colnames(iris), "(?i)species")
    

提交回复
热议问题