Skip to content

Commit

Permalink
between is vectorised. closes Rdatatable#534.
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsrinivasan authored and tangjian.li committed Aug 13, 2017
1 parent be47bdb commit 539a684
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions R/between.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

between <- function(x,lower,upper,incbounds=TRUE)
{
between <- function(x,lower,upper,incbounds=TRUE) {
if(incbounds) x>=lower & x<=upper
else x>lower & x<upper
}

"%between%" <- function(x,y) between(x,y[1],y[2],incbounds=TRUE)
# %between% is vectorised, #534.
"%between%" <- function(x,y) between(x,y[[1]],y[[2]],incbounds=TRUE)
# If we want non inclusive bounds with %between%, just +1 to the left, and -1 to the right (assuming integers)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@

32. Non-equi joins are now possible using the familiar `on=` syntax. With this, the set of binary operators extend from just `==` to `>=`, `>`, `<=`, `<` and `==`. For e.g., `X[Y, on=.(a, b>b)]` looks for `X.a == Y.a` first and within those matching rows for rows where`X.b > Y.b`. Partly addreses [#1452](https://github.com/Rdatatable/data.table/issues/1452).

33. `%between%` is vectorised which means we can now do: `DT[x %between% list(y,z)]` which is equivalent to `DT[x >= y & x <= z]`, [#534](https://github.com/Rdatatable/data.table/issues/534). Thanks @MicheleCarriero for filing the issue and the idea.

#### BUG FIXES

1. Now compiles and runs on IBM AIX gcc. Thanks to Vinh Nguyen for investigation and testing, [#1351](https://github.com/Rdatatable/data.table/issues/1351).
Expand Down
14 changes: 8 additions & 6 deletions man/between.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
\alias{\%between\%}
\title{ Convenience function for range subset logic. }
\description{
Intended for use in \code{i} in \code{[.data.table}.
Intended for use in \code{i} in \code{[.data.table}. From \code{v1.9.8}, \code{between} is vectorised.
}
\usage{
between(x,lower,upper,incbounds=TRUE)
x \%between\% y
}
\arguments{
\item{x}{ Any orderable vector, i.e., those with relevant methods for \code{`<=`}, such as \code{numeric}, \code{character}, \code{Date}, ... }
\item{lower}{ Lower range bound. }
\item{upper}{ Upper range bound. }
\item{y}{ A length-2 vector, with \code{y[1]} interpreted as \code{lower} and \code{y[2]} as \code{upper}. }
\item{lower}{ Lower range bound. Usually of length=\code{1} or \code{length(x)}.}
\item{upper}{ Upper range bound. Usually of same length as \code{lower}.}
\item{y}{ A length-2 \code{vector} or \code{list}, with \code{y[[1]]} interpreted as \code{lower} and \code{y[[2]]} as \code{upper}.}
\item{incbounds}{ \code{TRUE} means inclusive bounds, i.e., [lower,upper]. \code{FALSE} means exclusive bounds, i.e., (lower,upper). }
}
% \details{
Expand All @@ -24,7 +24,9 @@ x \%between\% y
\note{ Current implementation does not make use of ordered keys. \code{incbounds} is set to \code{TRUE} for the infix notation \code{\%between\%}. }
\seealso{ \code{\link{data.table}}, \code{\link{like}} }
\examples{
DT = data.table(a=1:5, b=6:10)
DT[b \%between\% c(7,9)]
DT = data.table(x=1:5, y=6:10, z=c(5:1))
DT[y \%between\% c(7,9)]
# NEW feature in v1.9.8, vectorised between
DT[z \%between\% list(x,y)]
}
\keyword{ data }

0 comments on commit 539a684

Please sign in to comment.