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. I currently have this in my local R startup script: `%between%` <- function(x,...) { y <- range( unlist(c(...)) ) return( x >= y[1] & x =< y[2] ) } It allows me to do things like: 5 %between c(1,10) and also as act as an "in_range" operator: foo %between% a.long.list.with.many.values This may seem unnecessary, since 5 >= foo[1] && foo<= foo[2] is also quite short to type, but there is a mental cost to this, eg if you are deeply focused on a complicated program flow, the %between% construct is a lot easier to type out, and relate to, than the logically more complex construct with && and <=/>=, at least in my experience. -- mvh Torbj?rn Lindahl [[alternative HTML version deleted]]
On 04/09/2014 10:41 AM, Torbj?rn Lindahl 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.But it appears that you do:> > I currently have this in my local R startup script: > > `%between%` <- function(x,...) { > y <- range( unlist(c(...)) ) > return( x >= y[1] & x =< y[2] ) > } > > It allows me to do things like: 5 %between c(1,10) > > and also as act as an "in_range" operator: > foo %between% a.long.list.with.many.valuesSo what you are asking is that someone should make this available to others, as well. That seems like a reasonable thing to do, but why shouldn't that someone be you?> > This may seem unnecessary, since 5 >= foo[1] && foo<= foo[2] is also quite > short to type, but there is a mental cost to this, eg if you are deeply > focused on a complicated program flow, the %between% construct is a lot > easier to type out, and relate to, than the logically more complex > construct with && and <=/>=, at least in my experience. >One problem with your definition is that it's not clear it does the right thing when x is a vector. I might have a vector of lower bounds, and a vector of upper bounds, and want to check each element of x against the corresponding bound, i.e. compute lower <= x & x <= upper Your %between% operator could be rewritten so that x %between% cbind(lower, upper) would give this result, but it doesn't do so now. (I'm not saying you *should* rewrite it like that, but it's something you should consider.) Duncan Murdoch
The TeachingDemos package has %<% and %<=% operators for a between style comparison. So for your example you could write: 1 %<% 5 %<% 10 or 1 %<=% 5 %<=% 10 And these operators already work with vectors: lb %<=% x %<% ub and can even be further chained: 0 %<% x %<% y %<% z %<% 1 # only points where x < y and y < z and all between 0 and 1. It is a little bit different syntax from yours, but would that do what you want? If not, we could add a %between% function (expand it a bit following Duncan's suggestion) to the TeachingDemos package if you don't want to create your own package. On Thu, Sep 4, 2014 at 8: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. > > I currently have this in my local R startup script: > > `%between%` <- function(x,...) { > y <- range( unlist(c(...)) ) > return( x >= y[1] & x =< y[2] ) > } > > It allows me to do things like: 5 %between c(1,10) > > and also as act as an "in_range" operator: > foo %between% a.long.list.with.many.values > > This may seem unnecessary, since 5 >= foo[1] && foo<= foo[2] is also quite > short to type, but there is a mental cost to this, eg if you are deeply > focused on a complicated program flow, the %between% construct is a lot > easier to type out, and relate to, than the logically more complex > construct with && and <=/>=, at least in my experience. > > -- > mvh > Torbj?rn Lindahl > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
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) > betweenfunction (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) > betweenfunction (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