[R] overlapping intervals
jim holtman
jholtman at gmail.com
Sat Feb 2 01:07:43 CET 2008
Here is one way of doing it:
> c.i <- read.table(textConnection(" 17130612 17587118
+ 17712302 18221688
+ 21225764 21387314
+ 25012714 30748348
+ 33852816 34480192
+ 36012944 36209144
+ 36252300 36280276
+ 36737468 36971144
+ 43693832 43878548"))
>
> d.i <- read.table(textConnection(" 17712302 18100404
+ 21203780 21387314
+ 25012714 30748348
+ 33852816 34384588
+ 34794536 35996440"))
> closeAllConnections()
>
> # setup data.frame for comparing
> x <- rbind(data.frame(t=c.i$V1, oper=1, type='c'),
+ data.frame(t=c.i$V2, oper=-1, type='c'),
+ data.frame(t=d.i$V1,oper=1, type='d'),
+ data.frame(t=d.i$V2, oper=-1, type='d'))
>
> # put in time order
> x <- x[order(x$t),]
> # determine overlaps
> x$over <- cumsum(x$oper)
> x
t oper type over
1 17130612 1 c 1
10 17587118 -1 c 0
2 17712302 1 c 1
19 17712302 1 d 2
24 18100404 -1 d 1
11 18221688 -1 c 0
20 21203780 1 d 1
3 21225764 1 c 2
12 21387314 -1 c 1
25 21387314 -1 d 0
4 25012714 1 c 1
21 25012714 1 d 2
13 30748348 -1 c 1
26 30748348 -1 d 0
5 33852816 1 c 1
22 33852816 1 d 2
27 34384588 -1 d 1
14 34480192 -1 c 0
23 34794536 1 d 1
28 35996440 -1 d 0
6 36012944 1 c 1
15 36209144 -1 c 0
7 36252300 1 c 1
16 36280276 -1 c 0
8 36737468 1 c 1
17 36971144 -1 c 0
9 43693832 1 c 1
18 43878548 -1 c 0
> # assuming that c & d don't overlap themselves, oper=2 indicate an overlap
> overlap <- which(x$over == 2)
> # print overlaps
> for (i in overlap){
+ print(x[i + c(-1,0,1,2),])
+ }
t oper type over
2 17712302 1 c 1
19 17712302 1 d 2
24 18100404 -1 d 1
11 18221688 -1 c 0
t oper type over
20 21203780 1 d 1
3 21225764 1 c 2
12 21387314 -1 c 1
25 21387314 -1 d 0
t oper type over
4 25012714 1 c 1
21 25012714 1 d 2
13 30748348 -1 c 1
26 30748348 -1 d 0
t oper type over
5 33852816 1 c 1
22 33852816 1 d 2
27 34384588 -1 d 1
14 34480192 -1 c 0
>
On Feb 1, 2008 4:03 PM, mohamed nur anisah <nuranisah_mohamed at yahoo.com> wrote:
> hi!!
>
> Below I have 4 columns vector of c and d which are unequal in length.These c and d have 2 columns each where these 2 columns represent an interval values. How am I going to get an overlapping over these interval values?? Please help me sort this problem!! Thanks in advance..
>
> c d
> 17130612 17587118 17712302 18100404
> 17712302 18221688 21203780 21387314
> 21225764 21387314 25012714 30748348
> 25012714 30748348 33852816 34384588
> 33852816 34480192 34794536 35996440
> 36012944 36209144
> 36252300 36280276
> 36737468 36971144
> 43693832 43878548
>
>
> ---------------------------------
>
> [[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