[R] find overlap between intervals

jim holtman jholtman at gmail.com
Wed Oct 31 19:55:26 CET 2007


Got a little bit of a different answer:

> x <- "Start End
+ 440   443
+ 380   443
+ 290   468"
> x.in <- read.table(textConnection(x), header=TRUE)
> # create matrix to determine queue size (overlap)
> x.q <- rbind(cbind(x.in$Start, 1), cbind(x.in$End, -1))
> # sort
> x.q <- x.q[order(x.q[,1], x.q[,2]),]
> x.q <- cbind(x.q, queue=cumsum(x.q[,2]))
> # remove duplicated entries
> x.q <- x.q[!duplicated(x.q[,1]),]
> x.q
            queue
[1,] 290  1     1
[2,] 380  1     2
[3,] 440  1     3
[4,] 443 -1     2
[5,] 468 -1     0
> # now output ranges -- '0' indicates end of an interval
> x.intervals <- t(sapply(seq_len(nrow(x.q) - 1), function(.row){
+     if (x.q[.row, 'queue'] == 0) next # skip
+     c(Start=x.q[.row, 1], End=x.q[.row + 1, 1])
+ }))
> x.intervals
     Start End
[1,]   290 380
[2,]   380 440
[3,]   440 443
[4,]   443 468
>


On 10/31/07, João Fadista <Joao.Fadista at agrsci.dk> wrote:
> Dear all,
>
> I would like to be able to know the intervals of my data that overlap between them. Here it goes a small example:
>
> Input:
> Start End
> 440   443
> 380   443
> 290   468
>
> Desired output:
> Start End
> 290  380
> 380  440
> 440  468
>
>
> Best regards,
> João Fadista
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list