Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive search of a list in R

Tags:

r

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.

like image 368
SFun28 Avatar asked Apr 15 '11 02:04

SFun28


People also ask

How do you make a case insensitive in R?

Construct a case-insensitive regex to "TRIP" by calling regex() with ignore_case = TRUE . Assign the result to trip_pattern . Repeat your viewing of catcident trips, this time using the case insensitive trip_pattern . You should get a few more hits.

How do you grep a case insensitive?

Case Insensitive Search By default, grep is case sensitive. This means that the uppercase and lowercase characters are treated as distinct. To ignore case when searching, invoke grep with the -i option (or --ignore-case ).

Is R merge case sensitive?

As we know R is case sensitive we should remember that FALSE and TRUE should be in upper case. Also, the name of the column in by should be exactly as in the data frame. The below example shows the error when you pass “CustomerID” to “CustomerId” in merge() function.

Why is R case sensitive?

It is case sensitive as are most UNIX based packages, so A and a are different symbols and would refer to different variables. The set of symbols which can be used in R names depends on the operating system and country within which R is being run (technically on the locale in use).


2 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.

like image 130
Farrel Avatar answered Sep 19 '22 22:09

Farrel


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.

like image 45
Aniko Avatar answered Sep 20 '22 22:09

Aniko