like: Convenience function for calling grep.

View source: R/like.R

likeR Documentation

Convenience function for calling grep.

Description

Intended for use in i in [.data.table, i.e., for subsetting/filtering.

Syntax should be familiar to SQL users, with interpretation as regex.

Usage

like(vector, pattern, ignore.case = FALSE, fixed = FALSE, perl = FALSE)
vector %like% pattern
vector %ilike% pattern
vector %flike% pattern
vector %plike% pattern

Arguments

vector

Either a character or a factor vector.

pattern

Pattern to be matched

ignore.case

logical; is pattern case-sensitive?

fixed

logical; should pattern be interpreted as a literal string (i.e., ignoring regular expressions)?

perl

logical; is pattern Perl-compatible regular expression?

Details

Internally, like is essentially a wrapper around base::grepl, except that it is smarter about handling factor input (base::grep uses slow as.character conversion).

Value

Logical vector, TRUE for items that match pattern.

Note

Current implementation does not make use of sorted keys.

See Also

base::grepl

Examples

DT = data.table(Name=c("Mary","George","Martha"), Salary=c(2,3,4))
DT[Name %like% "^Mar"]
DT[Name %ilike% "mar"]
DT[Name %flike% "Mar"]
DT[Name %plike% "(?=Ma)(?=.*y)"]

Rdatatable/data.table documentation built on Nov. 20, 2024, 6:44 p.m.