one way is:

list1 <- as.data.frame(list(open=c(1,5), close=c(2,10)))
list2 <- as.data.frame(list(open=c(1.5,3), close=c(2.5,10)))

data.frame(
    open = pmax(list1$open, list2$open),
    close = pmin(list1$close, list2$close)
)


I hope it helps.

Best,
Dimitris


Thomas Meyer wrote:
Hi,

Algorithm question: I have two sets of "intervals", where an interval is an ordered pair [a,b] of two numbers. Is there an efficient way in R to generate the intersection of two lists of same?

For concreteness: I'm representing a set of intervals with a data.frame:

 > list1 = as.data.frame(list(open=c(1,5), close=c(2,10)))
 > list1
  open close
1    1     2
2    5    10

 > list2 = as.data.frame(list(open=c(1.5,3), close=c(2.5,10)))
 > list2
  open close
1  1.5   2.5
2  3.0  10.0

How do I get the intersection which would be something like:
  open close
1  1.5   2.0
2  5.0  10.0

I wonder if there's some ready-built functionality that might help me out. I'm new to R and am still learning to vectorize my code and my thinking. Or maybe there's a package for interval arithmetic that I can just pull off the shelf.

Thanks,

-tom


--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to