Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include PostgreSQL's %ilike% into data.table #2519

Closed
andschar opened this issue Dec 13, 2017 · 3 comments
Closed

include PostgreSQL's %ilike% into data.table #2519

andschar opened this issue Dec 13, 2017 · 3 comments
Milestone

Comments

@andschar
Copy link

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%
@MichaelChirico
Copy link
Member

MichaelChirico commented Dec 14, 2017

You could use the (?i) tag instead:

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

@andschar
Copy link
Author

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.

@MichaelChirico
Copy link
Member

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants