[R] Methods/package for working with sets and intervals

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Jul 8 19:21:28 CEST 2002


Henrik Bengtsson <hb at maths.lth.se> writes:

> Before reinventing the wheel, is there a package for working on (nice) 
> sets and intervals, where one can for instance check if a set of intervals
> contains a scalar, taking the union and intersection of some intervals
> etc?
> 
>  Example:
> 
>    # Defining the set i = [1,2) + [3.5, 10]
>    i <- c(1,2, 3.5, 10)
>    attr(i, "include") <- c(TRUE, FALSE, TRUE, TRUE)
> 
>    x <- 0:12
> 
>    # Get the x's that are within the set i.
>    x[contains(i, x)]  # 1 4 5 6 7 8 9 10
>    
>    # Defining the set j = (-Inf,2] + [10, +Inf)
>    j <- c(-Inf,2, 10, +Inf)
>    attr(j, "include") <- c(NA, TRUE, TRUE, NA)
> 
>    k <- intersection(i,j)
>    # k = [1,2) + [10,10]

Not quite the same wheel, but you might consider overloading union()
and friends. 

For specifying the interval set, I think I would consider something
closer in style to Surv(), i.e. in your first case something like

Intv(left=c(1,3.5),right=c(2,10),include.l=TRUE,include.r=c(FALSE, TRUE))

or maybe -- if you're sure this is only to be used for sets (as
opposed to vectors of intervals)

union(interval.co(1,2), interval.cc(3.5,10))

(Just some thoughts, of course)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list