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.
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.
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 ).
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With