0

How can I find the intersection percentages of some continuous variables, see the example below, please?

d1<-data.frame(Start=c(10, 8, 6, 4 ), End=c(14, 12, 9,17 ))

I want to check each row of the columns A and B overlap with the rest of the row, instead of for loop? For example,

d1[1,] %overlaps% d1[2,] 

and d1[1,] %overlaps% d1[3,]and ..finally, d3[1,] %overlaps% d4[3,]

How to do that?

Melih Aras
  • 369
  • 1
  • 9

2 Answers2

0

Something like the following determines if one segment in the real line defined by the endpoints Start and End overlaps another segment. The row combinations are created with combn and an anonymous function is applied to each combination.

`%overlaps%` <- function(X, Y){
  f <- function(x, y){
    a1 <- x[1] <= y[1] && y[1] <= x[2]
    a2 <- x[1] <= y[2] && y[2] <= x[2]
    a1 || a2
  }
  f(X, Y) || f(Y, X)
}

combn(1:nrow(d1), 2, function(x) {
  d1[x[1], ] %overlaps% d1[x[2], ]
})
#[1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0

Note that Start in d1 has been given in a descending order, you only need to check if End values of next intervals are greater than the current Start value, e.g.,

> unlist(sapply(1:(nrow(d1)-1),function(k) d1$End[-(1:k)]>=d1$Start[k]))
[1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81