R/like.R

like = 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))
    } else {
        # most usually character, but integer and numerics will be silently coerced by grepl
        grepl(pattern,vector)
    }
    # returns 'logical' so can be combined with other where clauses.
}

"%like%" = like

Try the data.table package in your browser

Any scripts or data that you put into this service are public.

data.table documentation built on May 2, 2019, 4:57 p.m.