[R] Intersection of two sets of intervals

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Apr 15 15:14:07 CEST 2009


Not of the self but still not complicated:

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

Intersec <- data.frame(Open = pmax(list1$open, list2$open), Close =
pmin(list1$close, list2$close))
Intersec[Intersec$Open > Intersec$Close, ] <- NA
Intersec

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Thomas Meyer
Verzonden: woensdag 15 april 2009 14:59
Aan: r-help at r-project.org
Onderwerp: [R] Intersection of two sets of intervals

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

-- 
Thomas Meyer

______________________________________________
R-help at 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.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.




More information about the R-help mailing list