Henrik Bengtsson
2002-Jul-08 15:08 UTC
[R] Methods/package for working with sets and intervals
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] ... Cheers Henrik Bengtsson Dept. of Mathematical Statistics @ Centre for Mathematical Sciences Lund Institute of Technology/Lund University, Sweden (+2h UTC) +46 46 2229611 (off), +46 708 909208 (cell), +46 46 2224623 (fax) h b @ m a t h s . l t h . s e, http://www.maths.lth.se/~hb/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I've never seen functions specifically for doing this. But maybe you could do some hackery with findInterval() ? Still not really convenient.... -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Mon, 8 Jul 2002, Henrik Bengtsson wrote:> 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] > > ... > > > Cheers > > Henrik Bengtsson > > Dept. of Mathematical Statistics @ Centre for Mathematical Sciences > Lund Institute of Technology/Lund University, Sweden (+2h UTC) > +46 46 2229611 (off), +46 708 909208 (cell), +46 46 2224623 (fax) > h b @ m a t h s . l t h . s e, http://www.maths.lth.se/~hb/ > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2002-Jul-08 17:21 UTC
[R] Methods/package for working with sets and intervals
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._