[R] Operator proposal: %between%

Gabor Grothendieck ggrothendieck at gmail.com
Fri Sep 5 10:36:57 CEST 2014


On Thu, Sep 4, 2014 at 10:41 AM, Torbjørn Lindahl
<torbjorn.lindahl at gmail.com> wrote:
> Not sure if this is the proper list to propose changes like this, if it
> passes constructive criticism, it would like to have a %between% operator
> in the R language.
>

There is a between function in the data.table package.

> library(data.table)
> between
function (x, lower, upper, incbounds = TRUE)
{
    if (incbounds)
        x >= lower & x <= upper
    else x > lower & x < upper
}

and also in the tis package which works differently:

> library(tis)
> between
function (y, x1, x2)
{
    y <- unclass(y)
    x1 <- unclass(x1)
    x2 <- unclass(x2)
    small <- pmin(x1, x2)
    large <- pmax(x1, x2)
    (y >= small) & (y <= large)
}

In addition, SQL has a between operator

> library(sqldf)
> sqldf("select * from BOD where Time between 3 and 5")
  Time demand
1    3   19.0
2    4   16.0
3    5   15.6



More information about the R-help mailing list