51

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 know exactly how the columns are cased. My thought was to search names(myDataFrame) in a case-insensitive manner to return the proper casing of the column.

7 Answers 7

74

I would suggest the grep() function and some of its additional arguments that make it a pleasure to use.

grep("stringofinterest",names(dataframeofinterest),ignore.case=TRUE,value=TRUE)

without the argument value=TRUE you will only get a vector of index positions where the match occurred.

42

Assuming that there are no variable names which differ only in case, you can search your all-lowercase variable name in tolower(names(myDataFrame)):

match("b", tolower(c("A","B","C")))
[1] 2

This will produce only exact matches, but that is probably desirable in this case.

1
  • The searchable package which allows you to turn on case-insensitive matching.
    – ctbrown
    Commented Mar 21, 2017 at 10:12
21

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")
7
  • 2
    Also all modifiers from ?stringr::modifiers have ignore.case as the 2nd argument so here for example you can type str_detect(colnames(iris), fixed("species",ignore_case=TRUE)) Commented Jun 14, 2018 at 23:57
  • 1
    I looked into stringr' s doc and didn't find this behavior documented, where did you get this from ? Commented Jun 15, 2018 at 15:55
  • 1
    These are fairly standard regular expression modifiers: regular-expressions.info/modifiers.html
    – MrFlick
    Commented Jun 15, 2018 at 15:58
  • yes but grepl doesn't seem to support them so I assumed it was coded into stringr or stringi Commented Jun 15, 2018 at 16:19
  • 7
    grepl does with perl=TRUE: grepl("(?i)species", colnames(iris), perl=TRUE)
    – MrFlick
    Commented Jun 15, 2018 at 16:20
7

For anyone using this with %in%, simply use tolower on the right (or both) sides, like so:

"b" %in% c("a", "B", "c")
# [1] FALSE

tolower("b") %in% tolower(c("a", "B", "c"))
# [1] TRUE
2

The searchable package was created for allowing for various types of searching within objects:

l <- list( a=1, b=2, c=3 )
sl <- searchable(l)        # make the list "searchable"
sl <- ignore.case(sl)      # turn on case insensitivity

> sl['B']
$b
[1] 2

It works with lists and vectors and does a lot more than simple case-insensitive matching.

0

If you want to search for one set of strings in another set of strings, case insensitively, you could try:

s1 = c("a", "b")
s2 = c("B", "C")
matches = s1[ toupper(s1) %in% toupper(s2) ]
-1

Another way of achieving this is to use str_which(string, pattern) from the stringr package:

library("stringr")
str_which(string = tolower(colnames(iris)), pattern = "species")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.