Case-insensitive search of a list in R

前端 未结 7 1973
不知归路
不知归路 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:01

    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
    
    0 讨论(0)
  • 2020-11-30 05:09

    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 讨论(0)
  • 2020-11-30 05:14

    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) ]
    
    0 讨论(0)
  • 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")
    
    0 讨论(0)
  • 2020-11-30 05:18

    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.

    0 讨论(0)
  • 2020-11-30 05:21

    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.

    0 讨论(0)
提交回复
热议问题