Skip to content

psolymos/intrval

Repository files navigation

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 ([, ]) including 8 different combinations.

Values of x are compared to interval endpoints. Endpoints can be defined as a vector with two values: these values will be compared as a single interval with each value in \code{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.

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

Versions

Install CRAN version as:

install.packages("intrval")

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