I like the %like% operator in data.table quite a lot as I'm used to SQL queries. In PostgreSQL there is also the ILIKE option where the i stands for case-insensitive.
What do you think of including %ilike% into data.table?
To include this into data.table I would create an operator %ilike% in ilike.R based on like.R adding ignore.case = TRUE to the grep() calls as follows:
ilike <- function(vector, pattern)
{
# Intended for use with a data.table 'where'
# Don't use * or % like SQL's like. Uses regexpr syntax - more powerful.
if (is.factor(vector)) {
as.integer(vector) %in% grep(pattern,levels(vector), ignore.case = TRUE)
} else {
# most usually character, but integer and numerics will be silently coerced by grepl
grepl(pattern,vector, ignore.case = TRUE)
}
# returns 'logical' so can be combined with other where clauses.
}
"%ilike%" = ilike
reproducible example:
require(data.table)
cars = data.table(cars = rownames(mtcars), mtcars)
cars[ cars %like% 'fiat' ] # no case-insensitive search possible
cars[ grep('fiat', cars, ignore.case = TRUE) ] # using comparably long grep
cars[ cars %ilike% 'fiat' ] # the new %ilike%
I like the
%like%operator in data.table quite a lot as I'm used to SQL queries. In PostgreSQL there is also the ILIKE option where the i stands for case-insensitive.What do you think of including
%ilike%into data.table?To include this into data.table I would create an operator
%ilike%in ilike.R based on like.R addingignore.case = TRUEto thegrep()calls as follows:reproducible example: