Giter VIP home page Giter VIP logo

intrval's Introduction

intrval: Relational Operators for Intervals

CRAN version CRAN RStudio mirror downloads Linux build Status Windows build status Code coverage status

Functions for evaluating if values of vectors are within intervals using a x %()% c(a, b) style notation for comparing values of x with (a, b) interval to get a < x < b evaluated. Interval endpoints can be open ((, )) or closed ([, ]).

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. If lengths do not match, shorter objects are recycled. Return values are logicals.

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
%[]% %)(% %[<]% %[>]%
%[)% %)[% %[<)% %[>)%
%(]% %](% %(<]% %(>]%
%()% %][% %(<)% %(>)%

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).

Operator Expression Condition
%[o]% c(a1, b1) %[o]% c(a2, b2) `a1 %[]% c(a2, b2)

%)o(% is used for the negation, directional evaluation is done via the operators %[<o]% 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 %notin%.

Versions

Install development version from GitHub:

library(devtools)
install_github("psolymos/intrval")

User visible changes are listed in the NEWS file.

Report a problem

Use the issue tracker to report a problem.

License

GPL-2

Examples

## 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

## simple case with integers
1:5 %[]% c(2,4)
# [1] FALSE  TRUE  TRUE  TRUE FALSE
1:5 %[)% c(2,4)
# [1] FALSE  TRUE  TRUE FALSE FALSE
1:5 %(]% c(2,4)
# [1] FALSE FALSE  TRUE  TRUE FALSE
1:5 %()% c(2,4)
# [1] FALSE FALSE  TRUE FALSE FALSE

1:5 %][% c(2,4)
# [1]  TRUE  TRUE FALSE  TRUE  TRUE
1:5 %](% c(2,4)
# [1]  TRUE  TRUE FALSE FALSE  TRUE
1:5 %)[% c(2,4)
# [1]  TRUE FALSE FALSE  TRUE  TRUE
1:5 %)(% c(2,4)
# [1]  TRUE FALSE FALSE FALSE  TRUE

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

## numeric
((1:5)+0.5) %[]% (c(2,4)+0.5)
# [1] FALSE  TRUE  TRUE  TRUE FALSE

## character
c('a','b','c','d','e') %[]% c('b','d')
# [1] FALSE  TRUE  TRUE  TRUE FALSE

## ordered
as.ordered(c('a','b','c','d','e')) %[]% c('b','d')
# [1] FALSE  TRUE  TRUE  TRUE FALSE

## factor -- leads to NA with warnings
as.factor(c('a','b','c','d','e')) %[]% c('b','d')
# [1] NA NA NA NA NA
# Warning messages:
# 1: In Ops.factor(x, a) : ‘>=’ not meaningful for factors
# 2: In Ops.factor(x, b) : ‘<=’ not meaningful for factors

## dates
as.Date(1:5,origin='2000-01-01') %[]% as.Date(c(2,4),origin='2000-01-01')
# [1] FALSE  TRUE  TRUE  TRUE FALSE

## testing overlap
cbind(rep(3,5),rep(4,5)) %[o]% cbind(1:5, 2:6)
# [1] FALSE  TRUE  TRUE  TRUE FALSE
cbind(rep(3,5),rep(4,5)) %[<o]% cbind(1:5, 2:6)
# [1] FALSE FALSE FALSE FALSE  TRUE
cbind(rep(3,5),rep(4,5)) %[o>]% cbind(1:5, 2:6)
# [1]  TRUE FALSE FALSE FALSE FALSE

intrval's People

Contributors

psolymos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.