[This article was first published on Peter Solymos - R related posts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I recently posted a piece about how to write and document special functions in R. I meant that as a prelude for the topic I am writing about in this post. Let me start at the beginning. The other day Dirk Eddelbuettel tweeted about the new release of the data.table package (v1.9.8). There were new features announced for joins based on %inrange% and %between%. That got me thinking: it would be really cool to generalize this idea for different intervals, for example as x %[]% c(a, b).

## Motivation

We want to evaluate if values of x satisfy the condition x >= a & x <= b given that a <= b. Typing x %[]% c(a, b) instead of the previous expression is not much shorter (14 vs. 15 characters with counting spaces). But considering the a <= b condition as well, it becomes a saving (x >= min(a, b) & x <= mmax(a, b) is 31 characters long). And sorting is really important, because by flipping a and b, we get quite different answers:

x <- 5
x >= 1 & x <= 10
# [1] TRUE
x >= 10 & x <= 1
# [1] FALSE


Also, min and max will not be very useful when we want to vectorize the expression. We need to use pmin and pmax for obvious reasons:

x >= min(1:10, 10:1) & x <= max(10:1, 1:10)
# [1] TRUE
x >= pmin(1:10, 10:1) & x <= pmax(10:1, 1:10)
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE


If interval endpoints can also be open or closed, and allowing them to flip around makes the semantics of left/right closed/open interval definitions hard. We can thus all agree that there is a need for an expression, like x %[]% c(a, b), that is compact, flexible, and invariant to endpoint sorting. This is exactly what the intrval package is for!

## What’s in the package

Functions for evaluating if values of vectors are within different open/closed intervals (x %[]% c(a, b)), or if two closed intervals overlap (c(a1, b1) %[o]% c(a2, b2)). Operators for negation and directional relations also implemented.

### Value-to-interval relations

Values of x are compared to interval endpoints a and b (a <= b). Endpoints can be defined as a vector with two values (c(a, b)): these values will be compared as a single interval with each value in x. If endpoints are stored in a matrix-like object or a list, comparisons are made element-wise.

x <- rep(4, 5)
a <- 1:5
b <- 3:7
cbind(x=x, a=a, b=b)
x %[]% cbind(a, b) # matrix
x %[]% data.frame(a=a, b=b) # data.frame
x %[]% list(a, b) # list


If lengths do not match, shorter objects are recycled. Return values are logicals. Note: interval endpoints are sorted internally thus ensuring the condition a <= b is not necessary.

These value-to-interval operators work for numeric (integer, real) and ordered vectors, and object types which are measured at least on ordinal scale (e.g. dates).

#### Closed and open intervals

The following special operators are used to indicate closed ([, ]) or open ((, )) interval endpoints:

Operator Expression Condition
%[]% x %[]% c(a, b) x >= a & x <= b
%[)% x %[)% c(a, b) x >= a & x < b
%(]% x %(]% c(a, b) x > a & x <= b
%()% x %()% c(a, b) x > a & x < b

#### Negation and directional relations

Eqal Not equal Less than Greater than
%[]% %)(% %[<]% %[>]%
%[)% %)[% %[<)% %[>)%
%(]% %](% %(<]% %(>]%
%()% %][% %(<)% %(>)%

The helper function intrval_types can be used to print/plot the following summary:

### Interval-to-interval relations

The overlap of two closed intervals, [a1, b1] and [a2, b2], is evaluated by the %[o]% operator (a1 <= b1, a2 <= b2). Endpoints can be defined as a vector with two values (c(a1, b1))or can be stored in matrix-like objects or a lists in which case comparisons are made element-wise. Note: interval endpoints are sorted internally thus ensuring the conditions a1 <= b1 and a2 <= b2 is not necessary.

c(2:3) %[o]% c(0:1)
list(0:4, 1:5) %[o]% c(2:3)
cbind(0:4, 1:5) %[o]% c(2:3)
data.frame(a=0:4, b=1:5) %[o]% c(2:3)


If lengths do not match, shorter objects are recycled. These value-to-interval operators work for numeric (integer, real) and ordered vectors, and object types which are measured at least on ordinal scale (e.g. dates).

%)o(% is used for the negation, directional evaluation is done via the operators %[ and %[o>]%.

 Eqal Not equal Less than Greater than %[0]% %)0(% %[<0]% %[0>]% Operators for discrete variables The previous operators will return NA for unordered factors. Set overlap can be evaluated by the base %in% operator and its negation %nin%. (This feature is really redundant, I know, but decided to include regardless…) Install Install development version from GitHub (not yet on CRAN): library(devtools) install_github("psolymos/intrval") The package is licensed under GPL-2. Examples library(intrval) ## bounding box set.seed(1) n <- 10^4 x <- runif(n, -2, 2) y <- runif(n, -2, 2) d <- sqrt(x^2 + y^2) iv1 <- x %[]% c(-0.25, 0.25) & y %[]% c(-1.5, 1.5) iv2 <- x %[]% c(-1.5, 1.5) & y %[]% c(-0.25, 0.25) iv3 <- d %()% c(1, 1.5) plot(x, y, pch = 19, cex = 0.25, col = iv1 + iv2 + 1, main = "Intersecting bounding boxes") plot(x, y, pch = 19, cex = 0.25, col = iv3 + 1, main = "Deck the halls:\ndistance range from center") ## time series filtering x <- seq(0, 4*24*60*60, 60*60) dt <- as.POSIXct(x, origin="2000-01-01 00:00:00") f <- as.POSIXlt(dt)$hour %[]% c(0, 11) plot(sin(x) ~ dt, type="l", col="grey", main = "Filtering date/time objects") points(sin(x) ~ dt, pch = 19, col = f + 1) ## QCC library(qcc) data(pistonrings) mu <- mean(pistonrings$diameter[pistonrings$trial]) SD <- sd(pistonrings$diameter[pistonrings$trial]) x <- pistonrings$diameter[!pistonrings$trial] iv <- mu + 3 * c(-SD, SD) plot(x, pch = 19, col = x %)(% iv +1, type = "b", ylim = mu + 5 * c(-SD, SD), main = "Shewhart quality control chart\ndiameter of piston rings") abline(h = mu) abline(h = iv, lty = 2) ## Annette Dobson (1990) "An Introduction to Generalized Linear Models". ## Page 9: Plant Weight Data. ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2, 10, 20, labels = c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) ## compare 95% confidence intervals with 0 (CI.D9 <- confint(lm.D9)) # 2.5 % 97.5 % # (Intercept) 4.56934 5.4946602 # groupTrt -1.02530 0.2833003 0 %[]% CI.D9 # (Intercept) groupTrt # FALSE TRUE lm.D90 <- lm(weight ~ group - 1) # omitting intercept ## compare 95% confidence of the 2 groups to each other (CI.D90 <- confint(lm.D90)) # 2.5 % 97.5 % # groupCtl 4.56934 5.49466 # groupTrt 4.19834 5.12366 CI.D90[1,] %[o]% CI.D90[2,] # 2.5 % # TRUE DATE <- as.Date(c("2000-01-01","2000-02-01", "2000-03-31")) DATE %[<]% as.Date(c("2000-01-151", "2000-03-15")) # [1] TRUE FALSE FALSE DATE %[]% as.Date(c("2000-01-151", "2000-03-15")) # [1] FALSE TRUE FALSE DATE %[>]% as.Date(c("2000-01-151", "2000-03-15")) # [1] FALSE FALSE TRUE For more examples, see the unit-testing script. Feedback Please check out the package and use the issue tracker to suggest a new feature or report a problem. var vglnk = {key: '949efb41171ac6ec1bf7f206d57e90b8'}; (function(d, t) { var s = d.createElement(t); s.type = 'text/javascript'; s.async = true; // s.defer = true; // s.src = '//cdn.viglink.com/api/vglnk.js'; s.src = 'https://www.r-bloggers.com/wp-content/uploads/2020/08/vglnk.js'; var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script')); Related ShareTweet To leave a comment for the author, please follow the link and comment on their blog: Peter Solymos - R related posts. R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.       function init() { var vidDefer = document.getElementsByTagName('iframe'); for (var i=0; i<vidDefer.length; i++) { if(vidDefer[i].getAttribute('data-src')) { vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src')); } } } window.onload = init; R bloggers Facebook page Most viewed posts (weekly) Complexity is a source of income in open source ecosystems Little useless-useful R functions – Looping through variable names and generating plots April 2021: "Top 40" New CRAN Packages Build and improve a Machine Learning Classification model with TidyModels and R How to find dataset differences in R Quickly Compare Datasets Correlation in R ( NA friendliness, accepting matrix as input data, returning p values, visualization and Pearson vs Spearman) Stringr in r 10 data manipulation Tips and Tricks Sponsors // https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-my-script-s-in-Automatic-Mode- // this must be placed higher. Otherwise it doesn't work. // data-cfasync="false" is for making sure cloudflares' rocketcache doesn't interfeare with this // in this case it only works because it was used at the original script in the text widget function createCookie(name,value,days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + value + expires + "; path=/"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function eraseCookie(name) { createCookie(name,"",-1); } // no longer use async because of google // async async function readTextFile(file) { // Helps people browse between pages without the need to keep downloading the same // ads txt page everytime. This way, it allows them to use their browser's cache. var random_number = readCookie("ad_random_number_cookie"); if(random_number == null) { var random_number = Math.floor(Math.random()*100*(new Date().getTime()/10000000000)); createCookie("ad_random_number_cookie",random_number,1) } file += '?t='+random_number; var rawFile = new XMLHttpRequest(); rawFile.onreadystatechange = function () { if(rawFile.readyState === 4) { if(rawFile.status === 200 || rawFile.status == 0) { // var allText = rawFile.responseText; // document.write(allText); document.write(rawFile.responseText); } } } rawFile.open("GET", file, false); rawFile.send(null); } // readTextFile('https://raw.githubusercontent.com/Raynos/file-store/master/temp.txt'); readTextFile("https://www.r-bloggers.com/wp-content/uploads/text-widget_anti-cache.txt"); Recent Posts Tips And Tricks For Data Scientists Vol.8 GooglyPlusPlus2021 is now fully interactive!!! Bioconductor Asia Membership Increasing Due to Going Virtual Working with files and folders in R-Ultimate Guide Partial dependence plots with tidymodels and DALEX for #TidyTuesday Mario Kart world records A comparison of terra and raster packages Using {pagedown} in Docker A forecasting tool (API) with examples in curl, R, Python R-Ladies Philly – Building our Online Community During the Pandemic Correlation in R ( NA friendliness, accepting matrix as input data, returning p values, visualization and Pearson vs Spearman) Best Practices for R with Docker Learning R: Creating Truth Tables datatable editor-DT package in R Small gotcha when using negative indexing Map Symbols and Size Legends for leaflet Jobs for R-usersJunior Data Scientist / Quantitative economistSenior Quantitative AnalystR programmerData Scientist – CGIAR Excellence in Agronomy (Ref No: DDG-R4D/DS/1/CG/EA/06/20)Data Analytics Auditor, Future of Audit Lead @ London or Newcastle python-bloggers.com (python/data-science news)A forecasting tool (API) with examples in curl, R, PythonNaive Bayes Classification in NLP tasks from ScratchMarket Basket Analysis and Association Rules from ScratchCryptocurrency Trading Strategy by Detecting the Leaders and the FollowersHow to Find Similar Documents using N-grams and Word EmbeddingsStrong random forests with XGBoostTrees and networks Full list of contributing R-bloggers Archives Archives Select Month May 2021 (159) April 2021 (171) March 2021 (207) February 2021 (215) January 2021 (238) December 2020 (269) November 2020 (248) October 2020 (254) September 2020 (232) August 2020 (211) July 2020 (257) June 2020 (225) May 2020 (321) April 2020 (323) March 2020 (277) February 2020 (258) January 2020 (252) December 2019 (238) November 2019 (218) October 2019 (231) September 2019 (233) August 2019 (273) July 2019 (260) June 2019 (245) May 2019 (277) April 2019 (293) March 2019 (308) February 2019 (262) January 2019 (287) December 2018 (257) November 2018 (289) October 2018 (308) September 2018 (291) August 2018 (270) July 2018 (333) June 2018 (298) May 2018 (321) April 2018 (301) March 2018 (291) February 2018 (241) January 2018 (330) December 2017 (261) November 2017 (270) October 2017 (290) September 2017 (294) August 2017 (340) July 2017 (283) June 2017 (317) May 2017 (349) April 2017 (324) March 2017 (365) February 2017 (317) January 2017 (367) December 2016 (347) November 2016 (294) October 2016 (306) September 2016 (254) August 2016 (287) July 2016 (327) June 2016 (263) May 2016 (292) April 2016 (260) March 2016 (302) February 2016 (268) January 2016 (337) December 2015 (304) November 2015 (234) October 2015 (259) September 2015 (238) August 2015 (264) July 2015 (243) June 2015 (213) May 2015 (235) April 2015 (211) March 2015 (259) February 2015 (212) January 2015 (245) December 2014 (236) November 2014 (221) October 2014 (218) September 2014 (259) August 2014 (217) July 2014 (235) June 2014 (241) May 2014 (243) April 2014 (260) March 2014 (289) February 2014 (269) January 2014 (263) December 2013 (264) November 2013 (241) October 2013 (234) September 2013 (215) August 2013 (223) July 2013 (254) June 2013 (272) May 2013 (260) April 2013 (279) March 2013 (277) February 2013 (294) January 2013 (343) December 2012 (308) November 2012 (277) October 2012 (308) September 2012 (270) August 2012 (263) July 2012 (247) June 2012 (298) May 2012 (287) April 2012 (295) March 2012 (304) February 2012 (264) January 2012 (280) December 2011 (251) November 2011 (261) October 2011 (281) September 2011 (187) August 2011 (258) July 2011 (219) June 2011 (225) May 2011 (239) April 2011 (268) March 2011 (249) February 2011 (205) January 2011 (209) December 2010 (188) November 2010 (172) October 2010 (219) September 2010 (185) August 2010 (203) July 2010 (175) June 2010 (167) May 2010 (164) April 2010 (152) March 2010 (165) February 2010 (135) January 2010 (121) December 2009 (126) November 2009 (66) October 2009 (87) September 2009 (65) August 2009 (56) July 2009 (64) June 2009 (54) May 2009 (35) April 2009 (38) March 2009 (40) February 2009 (33) January 2009 (42) December 2008 (16) November 2008 (14) October 2008 (10) September 2008 (8) August 2008 (11) July 2008 (7) June 2008 (8) May 2008 (8) April 2008 (4) March 2008 (5) February 2008 (6) January 2008 (10) December 2007 (3) November 2007 (5) October 2007 (9) September 2007 (7) August 2007 (21) July 2007 (9) June 2007 (3) May 2007 (3) April 2007 (1) March 2007 (5) February 2007 (4) November 2006 (1) October 2006 (2) August 2006 (3) July 2006 (1) June 2006 (1) May 2006 (3) April 2006 (1) March 2006 (1) February 2006 (5) January 2006 (1) October 2005 (1) September 2005 (3) May 2005 (1) /* <![CDATA[ */ (function() { var dropdown = document.getElementById( "archives-dropdown-3" ); function onSelectChange() { if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) { document.location.href = this.options[ this.selectedIndex ].value; } } dropdown.onchange = onSelectChange; })(); /* ]]> */ Other sites Jobs for R-users SAS blogs     Copyright © 2021 | MH Corporate basic by MH Themes   [{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/www.r-bloggers.com#Organization","name":"R-bloggers","url":"https:\/\/www.r-bloggers.com","sameAs":[],"logo":{"@type":"ImageObject","url":"http:\/\/www.r-bloggers.com\/wp-content\/uploads\/2021\/05\/R_blogger_logo1_large.png","width":"1285","height":"369"},"contactPoint":{"@type":"ContactPoint","contactType":"technical support","telephone":"","url":"https:\/\/www.r-bloggers.com\/contact-us\/"}},{"@type":"WebSite","@id":"https:\/\/www.r-bloggers.com#website","headline":"R-bloggers","name":"R-bloggers","description":"R news and tutorials contributed by hundreds of R bloggers","url":"https:\/\/www.r-bloggers.com","potentialAction":{"@type":"SearchAction","target":"https:\/\/www.r-bloggers.com\/?s={search_term_string}","query-input":"required name=search_term_string"},"publisher":{"@id":"https:\/\/www.r-bloggers.com#Organization"}},{"@context":"https:\/\/schema.org","@type":"WebPage","@id":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/#webpage","name":"Relational operators for intervals with the intrval R package","url":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/","lastReviewed":"2016-12-02T01:00:00-06:00","inLanguage":"en-US","description":"I recently posted a piece about how to write and document special functions in R. I meant that as a prelude for the topic I am writing about in this post. Let me start at the beginning. The other day Dirk Eddelbuettel tweeted about the new release of the data.table package (v1.9.8).\nThere were new features announced for joins based on %inrange% and %between%. That got me thinking: it would be really cool to generalize this idea for different intervals, for example as x %% c(a, b).","reviewedBy":{"@type":"Organization","name":"R-bloggers","url":"https:\/\/www.r-bloggers.com","logo":{"@type":"ImageObject","url":"http:\/\/www.r-bloggers.com\/wp-content\/uploads\/2021\/05\/R_blogger_logo1_large.png","width":"1285","height":"369"}},"primaryImageOfPage":{"@id":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/#primaryimage"},"mainContentOfPage":[[{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"Home","url":"https:\/\/www.r-bloggers.com"},{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"About","url":"http:\/\/www.r-bloggers.com\/about\/"},{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"RSS","url":"https:\/\/feeds.feedburner.com\/RBloggers"},{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"add your blog!","url":"http:\/\/www.r-bloggers.com\/add-your-blog\/"},{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"Learn R","url":"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/"},{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"R jobs","url":"https:\/\/www.r-users.com\/"},{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"Submit a new job (it's free)","url":"https:\/\/www.r-users.com\/submit-job\/"},{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"Browse latest jobs (also free)","url":"https:\/\/www.r-users.com\/"},{"@context":"https:\/\/schema.org","@type":"SiteNavigationElement","@id":"https:\/\/www.r-bloggers.com\/#top nav","name":"Contact us","url":"http:\/\/www.r-bloggers.com\/contact-us\/"}]],"isPartOf":{"@id":"https:\/\/www.r-bloggers.com#website"},"breadcrumb":{"@id":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/#breadcrumb"}},{"@type":"BreadcrumbList","@id":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https:\/\/www.r-bloggers.com","name":"R-bloggers"}},{"@type":"ListItem","position":2,"item":{"@id":"https:\/\/www.r-bloggers.com\/category\/r-bloggers\/","name":"R bloggers"}},{"@type":"ListItem","position":3,"item":{"@id":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/","name":"Relational operators for intervals with the intrval R package"}}]},{"@type":"Article","@id":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/#Article","url":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/","inLanguage":"en-US","mainEntityOfPage":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/#webpage","headline":"Relational operators for intervals with the intrval R package","description":"I recently posted a piece about how to write and document special functions in R. I meant that as a prelude for the topic I am writing about in this post. Let me start at the beginning. The other day Dirk Eddelbuettel tweeted about the new release of the data.table package (v1.9.8).\nThere were new features announced for joins based on %inrange% and %between%. That got me thinking: it would be really cool to generalize this idea for different intervals, for example as x %% c(a, b).","articleBody":"I recently posted a piece about how to write and document special functions in R. I meant that as a prelude for the topic I am writing about in this post. Let me start at the beginning. The other day Dirk Eddelbuettel tweeted about the new release of the data.table package (v1.9.8). There were new features announced for joins based on %inrange% and %between%. That got me thinking: it would be really cool to generalize this idea for different intervals, for example as x %% c(a, b). Motivation We want to evaluate if values of x satisfy the condition x &gt; a &amp; x &lt; b given that a &lt; b. Typing x %% c(a, b) instead of the previous expression is not much shorter (14 vs. 15 characters with counting spaces). But considering the a &lt; b condition as well, it becomes a saving (x &gt; min(a, b) &amp; x &lt; mmax(a, b) is 31 characters long). And sorting is really important, because by flipping a and b, we get quite different answers: x &lt;- 5 x &gt; 1 &amp; x &lt; 10 # TRUE x &gt; 10 &amp; x &lt; 1 # FALSE Also, min and max will not be very useful when we want to vectorize the expression. We need to use pmin and pmax for obvious reasons: x &gt; min(1:10, 10:1) &amp; x &lt; max(10:1, 1:10) # TRUE x &gt; pmin(1:10, 10:1) &amp; x &lt; pmax(10:1, 1:10) # TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE If interval endpoints can also be open or closed, and allowing them to flip around makes the semantics of left\/right closed\/open interval definitions hard. We can thus all agree that there is a need for an expression, like x %% c(a, b), that is compact, flexible, and invariant to endpoint sorting. This is exactly what the intrval package is for! What\u2019s in the package Functions for evaluating if values of vectors are within different open\/closed intervals (x %% c(a, b)), or if two closed intervals overlap (c(a1, b1) %% c(a2, b2)). Operators for negation and directional relations also implemented. Value-to-interval relations Values of x are compared to interval endpoints a and b (a &lt; b). Endpoints can be defined as a vector with two values (c(a, b)): these values will be compared as a single interval with each value in x. If endpoints are stored in a matrix-like object or a list, comparisons are made element-wise. x &lt;- rep(4, 5) a &lt;- 1:5 b &lt;- 3:7 cbind(xx, aa, bb) x %% cbind(a, b) # matrix x %% data.frame(aa, bb) # data.frame x %% list(a, b) # list If lengths do not match, shorter objects are recycled. Return values are logicals. Note: interval endpoints are sorted internally thus ensuring the condition a &lt; b is not necessary. These value-to-interval operators work for numeric (integer, real) and ordered vectors, and object types which are measured at least on ordinal scale (e.g. dates). Closed and open intervals The following special operators are used to indicate closed () or open ((, )) interval endpoints: Operator Expression Condition %% x %% c(a, b) x &gt; a &amp; x &lt; b %[)% x %[)% c(a, b) x &gt; a &amp; x &lt; b %(]% x %(]% c(a, b) x &gt; a &amp; x &lt; b %()% x %()% c(a, b) x &gt; a &amp; x &lt; b Negation and directional relations Eqal Not equal Less than Greater than %% %)(% %% %% %[)% %)[% %[&lt;)% %[&gt;)% %(]% %](% %(&lt;]% %(&gt;]% %()% %][% %(&lt;)% %(&gt;)% The helper function intrval_types can be used to print\/plot the following summary: Interval-to-interval relations The overlap of two closed intervals, and , is evaluated by the %% operator (a1 &lt; b1, a2 &lt; b2). Endpoints can be defined as a vector with two values (c(a1, b1))or can be stored in matrix-like objects or a lists in which case comparisons are made element-wise. Note: interval endpoints are sorted internally thus ensuring the conditions a1 &lt; b1 and a2 &lt; b2 is not necessary. c(2:3) %% c(0:1) list(0:4, 1:5) %% c(2:3) cbind(0:4, 1:5) %% c(2:3) data.frame(a0:4, b1:5) %% c(2:3) If lengths do not match, shorter objects are recycled. These value-to-interval operators work for numeric (integer, real) and ordered vectors, and object types which are measured at least on ordinal scale (e.g. dates). %)o(% is used for the negation, directional evaluation is done via the operators %% and %%. Eqal Not equal Less than Greater than %% %)0(% %% %% Operators for discrete variables The previous operators will return NA for unordered factors. Set overlap can be evaluated by the base %in% operator and its negation %nin%. (This feature is really redundant, I know, but decided to include regardless\u2026) Install Install development version from GitHub (not yet on CRAN): library(devtools) install_github(\"psolymos\/intrval\") The package is licensed under GPL-2. Examples library(intrval) ## bounding box set.seed(1) n &lt;- 10^4 x &lt;- runif(n, -2, 2) y &lt;- runif(n, -2, 2) d &lt;- sqrt(x^2 + y^2) iv1 &lt;- x %% c(-0.25, 0.25) &amp; y %% c(-1.5, 1.5) iv2 &lt;- x %% c(-1.5, 1.5) &amp; y %% c(-0.25, 0.25) iv3 &lt;- d %()% c(1, 1.5) plot(x, y, pch 19, cex 0.25, col iv1 + iv2 + 1, main \"Intersecting bounding boxes\") plot(x, y, pch 19, cex 0.25, col iv3 + 1, main \"Deck the halls:\\ndistance range from center\") ## time series filtering x &lt;- seq(0, 4*24*60*60, 60*60) dt &lt;- as.POSIXct(x, origin\"2000-01-01 00:00:00\") f &lt;- as.POSIXlt(dt)$hour %% c(0, 11) plot(sin(x) ~ dt, type\"l\", col\"grey\", main \"Filtering date\/time objects\") points(sin(x) ~ dt, pch 19, col f + 1) ## QCC library(qcc) data(pistonrings) mu &lt;- mean(pistonrings$diameter) SD &lt;- sd(pistonrings$diameter) x &lt;- pistonrings$diameter iv &lt;- mu + 3 * c(-SD, SD) plot(x, pch 19, col x %)(% iv +1, type \"b\", ylim mu + 5 * c(-SD, SD), main \"Shewhart quality control chart\\ndiameter of piston rings\") abline(h mu) abline(h iv, lty 2) ## Annette Dobson (1990) \"An Introduction to Generalized Linear Models\". ## Page 9: Plant Weight Data. ctl &lt;- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt &lt;- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group &lt;- gl(2, 10, 20, labels c(\"Ctl\",\"Trt\")) weight &lt;- c(ctl, trt) lm.D9 &lt;- lm(weight ~ group) ## compare 95% confidence intervals with 0 (CI.D9 &lt;- confint(lm.D9)) # 2.5 % 97.5 % # (Intercept) 4.56934 5.4946602 # groupTrt -1.02530 0.2833003 0 %% CI.D9 # (Intercept) groupTrt # FALSE TRUE lm.D90 &lt;- lm(weight ~ group - 1) # omitting intercept ## compare 95% confidence of the 2 groups to each other (CI.D90 &lt;- confint(lm.D90)) # 2.5 % 97.5 % # groupCtl 4.56934 5.49466 # groupTrt 4.19834 5.12366 CI.D90 %% CI.D90 # 2.5 % # TRUE DATE &lt;- as.Date(c(\"2000-01-01\",\"2000-02-01\", \"2000-03-31\")) DATE %% as.Date(c(\"2000-01-151\", \"2000-03-15\")) # TRUE FALSE FALSE DATE %% as.Date(c(\"2000-01-151\", \"2000-03-15\")) # FALSE TRUE FALSE DATE %% as.Date(c(\"2000-01-151\", \"2000-03-15\")) # FALSE FALSE TRUE For more examples, see the unit-testing script. Feedback Please check out the package and use the issue tracker to suggest a new feature or report a problem.","keywords":"","datePublished":"2016-12-02T01:00:00-06:00","dateModified":"2016-12-02T01:00:00-06:00","author":{"@type":"Person","name":"Peter Solymos","description":"","url":"https:\/\/www.r-bloggers.com\/author\/peter-solymos\/","sameAs":["http:\/\/peter.solymos.org"],"image":{"@type":"ImageObject","url":"https:\/\/secure.gravatar.com\/avatar\/c4dc7254130e53333d88698a75112bc5?s=96&d=mm&r=g","height":96,"width":96}},"editor":{"@type":"Person","name":"Peter Solymos","description":"","url":"https:\/\/www.r-bloggers.com\/author\/peter-solymos\/","sameAs":["http:\/\/peter.solymos.org"],"image":{"@type":"ImageObject","url":"https:\/\/secure.gravatar.com\/avatar\/c4dc7254130e53333d88698a75112bc5?s=96&d=mm&r=g","height":96,"width":96}},"publisher":{"@id":"https:\/\/www.r-bloggers.com#Organization"},"image":[{"@type":"ImageObject","url":"https:\/\/github.com\/psolymos\/intrval\/raw\/master\/extras\/intrval.png","width":800,"height":400,"@id":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/#primaryimage"},{"@type":"ImageObject","url":"https:\/\/github.com\/psolymos\/intrval\/raw\/master\/extras\/examples.png","width":821,"height":802}],"isPartOf":{"@id":"https:\/\/www.r-bloggers.com\/2016\/12\/relational-operators-for-intervals-with-the-intrval-r-package\/#webpage"}}]}] var snp_f = []; var snp_hostname = new RegExp(location.host); var snp_http = new RegExp("^(http|https)://", "i"); var snp_cookie_prefix = ''; var snp_separate_cookies = false; var snp_ajax_url = 'https://www.r-bloggers.com/wp-admin/admin-ajax.php'; var snp_ajax_nonce = '9055af22ce'; var snp_ignore_cookies = false; var snp_enable_analytics_events = false; var snp_enable_mobile = false; var snp_use_in_all = false; var snp_excluded_urls = []; snp_excluded_urls.push(''); Never miss an update! Subscribe to R-bloggers to receive e-mails with the latest R posts. (You will not see this message again.) Click here to close (This popup will not appear again) .snp-pop-109583 .snp-theme6 { max-width: 700px;} .snp-pop-109583 .snp-theme6 h1 {font-size: 17px;} .snp-pop-109583 .snp-theme6 { color: #a0a4a9;} .snp-pop-109583 .snp-theme6 .snp-field ::-webkit-input-placeholder { color: #a0a4a9;} .snp-pop-109583 .snp-theme6 .snp-field :-moz-placeholder { color: #a0a4a9;} .snp-pop-109583 .snp-theme6 .snp-field :-ms-input-placeholder { color: #a0a4a9;} .snp-pop-109583 .snp-theme6 .snp-field input { border: 1px solid #a0a4a9;} .snp-pop-109583 .snp-theme6 .snp-field { color: #000000;} .snp-pop-109583 .snp-theme6 { background: #f2f2f2;} jQuery(document).ready(function() { }); var CaptchaCallback = function() { jQuery('.g-recaptcha').each(function(index, el) { grecaptcha.render(el, { 'sitekey' : '' }); }); }; /* <![CDATA[ */!function(e,n){var r={"selectors":{"block":"pre","inline":"code"},"options":{"indent":4,"ampersandCleanup":true,"linehover":true,"rawcodeDbclick":false,"textOverflow":"scroll","linenumbers":false,"theme":"enlighter","language":"r","retainCssClasses":false,"collapse":false,"toolbarOuter":"","toolbarTop":"{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}","toolbarBottom":""},"resources":["https:\/\/www.r-bloggers.com\/wp-content\/plugins\/enlighter\/cache\/enlighterjs.min.css?4WJrVky+dDEQ83W","https:\/\/www.r-bloggers.com\/wp-content\/plugins\/enlighter\/resources\/enlighterjs\/enlighterjs.min.js"]},o=document.getElementsByTagName("head")[0],t=n&&(n.error||n.log)||function(){};e.EnlighterJSINIT=function(){!function(e,n){var r=0,l=null;function c(o){l=o,++r==e.length&&(!0,n(l))}e.forEach(function(e){switch(e.match(/\.([a-z]+)(?:[#?].*)?$/)[1]){case"js":var n=document.createElement("script");n.onload=function(){c(null)},n.onerror=c,n.src=e,n.async=!0,o.appendChild(n);break;case"css":var r=document.createElement("link");r.onload=function(){c(null)},r.onerror=c,r.rel="stylesheet",r.type="text/css",r.href=e,r.media="all",o.appendChild(r);break;default:t("Error: invalid file extension",e)}})}(r.resources,function(e){e?t("Error: failed to dynamically load EnlighterJS resources!",e):"undefined"!=typeof EnlighterJS?EnlighterJS.init(r.selectors.block,r.selectors.inline,r.options):t("Error: EnlighterJS resources not loaded yet!")})},(document.querySelector(r.selectors.block)||document.querySelector(r.selectors.inline))&&e.EnlighterJSINIT()}(window,console); /* ]]> */ window.FPConfig= { delay: 0, ignoreKeywords: ["\/wp-admin","\/wp-login.php","\/cart","add-to-cart","logout","#","?",".png",".jpeg",".jpg",".gif",".svg"], maxRPS: 3, hoverDelay: 50 }; _stq = window._stq || []; _stq.push([ 'view', {v:'ext',j:'1:7.3.2',blog:'11524731',post:'144164',tz:'-6',srv:'www.r-bloggers.com'} ]); _stq.push([ 'clickTrackerInit', '11524731', '144164' ]); jQuery(document).ready(function ($) { //$( document ).ajaxStart(function() { //}); for (var i = 0; i < document.forms.length; ++i) { var form = document.forms[i]; if ($(form).attr("method") != "get") {$(form).append('<input type="hidden" name="iVyMkIflEKrpZQG" value="gJdt8zE4a_*W" />'); } if ($(form).attr("method") != "get") {$(form).append('<input type="hidden" name="sNPtrQh" value="BZuMIpOiA" />'); } } $(document).on('submit', 'form', function () { if ($(this).attr("method") != "get") { $(this).append('<input type="hidden" name="iVyMkIflEKrpZQG" value="gJdt8zE4a_*W" />'); } if ($(this).attr("method") != "get") { \$(this).append('<input type="hidden" name="sNPtrQh" value="BZuMIpOiA" />'); } return true; }); jQuery.ajaxSetup({ beforeSend: function (e, data) { //console.log(Object.getOwnPropertyNames(data).sort()); //console.log(data.type); if (data.type !== 'POST') return; if (typeof data.data === 'object' && data.data !== null) { data.data.append("iVyMkIflEKrpZQG", "gJdt8zE4a_*W"); data.data.append("sNPtrQh", "BZuMIpOiA"); } else { data.data = data.data + '&iVyMkIflEKrpZQG=gJdt8zE4a_*W&sNPtrQh=BZuMIpOiA'; } } }); });