Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the %like% operator to be case insensitive

Tags:

r

data.table

Is there a way to turn the %like% operator in the datatable package to be case insensitive ? So for instance 'hello' %like% 'HELlo' would match.

like image 782
ChiseledAbs Avatar asked Jan 02 '17 11:01

ChiseledAbs


1 Answers

By not relying on the definition in data.table:

`%like%` <- function (x, pattern) { 
  stringi::stri_detect_regex(x, pattern, case_insensitive=TRUE)
}

data.table defines it as:

function (vector, pattern) 
{
    if (is.factor(vector)) {
        as.integer(vector) %in% grep(pattern, levels(vector))
    }
    else {
        grepl(pattern, vector)
    }
}

You could cover the factor case if you like but it's not a very complex function. No "magic" in it.

I use stringi as it is (for my work) far more robust than built-in string ops and provides a great deal more power under the hood.

You can also define it as:

`%like%` <- function (x, pattern) { 
  grepl(pattern, x, ignore.case=TRUE)
}

(again, ignoring the factor case) if you like. You lose the vectorized pattern doing this, tho.

Make the name %likeic% (like, ignore case) if you don't want to squash the definition for data.table's %like%.

like image 82
hrbrmstr Avatar answered Nov 15 '22 00:11

hrbrmstr