Data.table: include PostgreSQL's %ilike% into data.table

Created on 13 Dec 2017  ·  3Comments  ·  Source: Rdatatable/data.table

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%
beginner-task

Most helpful comment

True that works as well, wasn't aware of such a regex. Nevertheless %ilike% seems a more intuitive to me. Maybe because I know PostgreSQL. Overall this is a question of coding style anyway.

All 3 comments

You could use the (?i) tag instead:

cars[cars %like% '(?i)fiat']

True that works as well, wasn't aware of such a regex. Nevertheless %ilike% seems a more intuitive to me. Maybe because I know PostgreSQL. Overall this is a question of coding style anyway.

@andreasLD thanks again for raising. You probably saw since you commented there as well but noting for the record that this was closed in #3333 and #3552

Was this page helpful?
0 / 5 - 0 ratings