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
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.
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.
-
The searchable package which allows you to turn on case-insensitive matching.– ctbrownCommented Mar 21, 2017 at 10:12
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")
-
2Also all modifiers from
?stringr::modifiers
haveignore.case
as the 2nd argument so here for example you can typestr_detect(colnames(iris), fixed("species",ignore_case=TRUE))
Commented Jun 14, 2018 at 23:57 -
1I 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 -
1These are fairly standard regular expression modifiers: regular-expressions.info/modifiers.html– MrFlickCommented Jun 15, 2018 at 15:58
-
yes but
grepl
doesn't seem to support them so I assumed it was coded intostringr
orstringi
Commented Jun 15, 2018 at 16:19 -
7
grepl
does withperl=TRUE
:grepl("(?i)species", colnames(iris), perl=TRUE)
– MrFlickCommented Jun 15, 2018 at 16:20
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
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.
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) ]
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")