-
Notifications
You must be signed in to change notification settings - Fork 990
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #3333 -- add %ilike% and %flike% filter operators
- Loading branch information
Michael Chirico
committed
May 9, 2019
1 parent
efbc171
commit 160b6ed
Showing
5 changed files
with
43 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
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. | ||
# Intended for use with a data.table 'where' | ||
# Don't use * or % like SQL's like. Uses regexpr syntax - more powerful. | ||
# returns 'logical' so can be combined with other where clauses. | ||
like <- function(vector, pattern, ignore.case = FALSE, fixed = FALSE) { | ||
if (is.factor(vector)) { | ||
as.integer(vector) %in% grep(pattern,levels(vector)) | ||
as.integer(vector) %in% grep(pattern, levels(vector), ignore.case = ignore.case, fixed = fixed) | ||
} else { | ||
# most usually character, but integer and numerics will be silently coerced by grepl | ||
grepl(pattern,vector) | ||
grepl(pattern, vector, ignore.case = ignore.case, fixed = fixed) | ||
} | ||
# returns 'logical' so can be combined with other where clauses. | ||
} | ||
|
||
"%like%" = like | ||
|
||
|
||
"%like%" = function(vector, pattern) like(vector, pattern) | ||
# as grep -i -- grep, ignoring case | ||
"%ilike%" = function(vector, pattern) like(vector, pattern, ignore.case = TRUE) | ||
# as grep -F or fgrep -- grep against a fixed pattern (no regex) | ||
# (more efficient where applicable) | ||
"%flike%" = function(vector, pattern) like(vector, pattern, fixed = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,38 @@ | ||
\name{like} | ||
\alias{like} | ||
\alias{\%like\%} | ||
\title{ Convenience function for calling regexpr. } | ||
\alias{\%ilike\%} | ||
\alias{\%flike\%} | ||
\title{ Convenience function for calling grep. } | ||
\description{ | ||
Intended for use in \code{i} in \code{[.data.table}. | ||
Intended for use in \code{i} in \code{\link[=data.table]{[.data.table}}, i.e., for subsetting/filtering. | ||
|
||
Syntax should be familiar to SQL users, with interpretation as regex. | ||
} | ||
\usage{ | ||
like(vector,pattern) | ||
like(vector, pattern, ignore.case = FALSE, fixed = FALSE) | ||
vector \%like\% pattern | ||
vector \%ilike\% pattern | ||
vector \%flike\% pattern | ||
} | ||
\arguments{ | ||
\item{vector}{ Either a \code{character} vector or a \code{factor}. A \code{factor} is faster. } | ||
\item{pattern}{ Passed on to \code{\link{grepl}}. } | ||
\item{vector}{ Either a \code{character} or a \code{factor} vector. } | ||
\item{pattern}{ Pattern to be matched } | ||
\item{ignore.case}{ \code{logical}; is \code{pattern} case-sensitive? } | ||
\item{fixed}{ \code{logical}; should \code{pattern} be interpreted as a literal string (i.e., ignoring regular expressions)? } | ||
} | ||
\details{ | ||
Internally, \code{like} is essentially a wrapper around \code{\link[base]{grepl}}, except that it is smarter about handling \code{factor} input (\code{base::grep} uses slow \code{as.character} conversion). | ||
} | ||
% \details{ | ||
% } | ||
\value{ | ||
Logical vector, \code{TRUE} for items that match \code{pattern}. | ||
} | ||
\note{ Current implementation does not make use of sorted keys. } | ||
\seealso{ \code{\link{data.table}}, \code{\link{grepl}} } | ||
\seealso{ \code{\link[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"] | ||
} | ||
\keyword{ data } |