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