I have a group of intervals for different ids. For example:
df <- data.frame(id=c(rep("a",4),rep("b",2),rep("c",3)), start=c(100,250,400,600,150,610,275,600,700), end=c(200,300,550,650,275,640,325,675,725))
The intervals of each id do not overlap but the intervals of the different ids may overlap. Here is a picture:
plot(range(df[,c(2,3)]),c(1,nrow(df)),type="n",xlab="",ylab="",yaxt="n")
for ( ii in 1:nrow(df) ) lines(c(df[ii,2],df[ii,3]),rep(nrow(df)-ii+1,2),col=as.numeric(df$id[ii]),lwd=2)
legend("bottomleft",lwd=2,col=seq_along(levels(df$id)),legend=levels(df$id))
What I'm looking for is for two functions: 1. A function which will take the union of these intervals. For the example above, it will return this data.frame:
union.df <- data.frame(id=rep("a,b,c",4), start=c(100,400,600,700), end=c(325,550,675,725))
- A function which will intersect these intervals, only keeping a range if all the ids overlap for that range. For the example above, it will return this data.frame:
intersection.df <- data.frame(id="a,b,c", start=610, end=640)
intersect
andunion
won't work - they work on discrete sets, not intervals. – Ladyship