'How to compare each row of a data frame to each row of another dataframe and calculate overlap

I have two dataframes with start and end time. I want to compare each row of df2 to each row of df1 and calculate the overlap.

df1
    #    start    end
    #1    5       15
    #2    20      28      
    #3    46      68     
    #4    80      87

df2
    #    start    end
    #1    20      40
    #2    65      85    

So the results should be a vector with results

overlaping_duration_1= 8 (overlap from df2 row 1 with df1 row 1)
overlaping_duration_2= 3+5 = overlap from df2 row 2 with df1 row 3 + overlap from df2 row 2 with df1 row 4

I tried it with an ifelse approach and cover the different conditions. This is only for the first row for df2.

overlap =  ifelse ( df2$start <= df1$start & df1$start <= df2$end & df2$end <= df1$end, df2$end-df1$start, 0)

overlap2 =  ifelse ( df2$start <= df1$start & df1$end <= df2$end, df1$end-df1$start, 0)

overlap3 =  ifelse ( df1$start < df2$start & df2$end <= df1$end, df2$end-df2$start, 0)

overlap4 =  ifelse ( df1$start < df2$start & df2$start <= df1$end & df1$end <= df2$end, df1$end-df2$start, 0)

Afterwards the different overlap vectors can be merged. This could be applied over a for loop over df2. This approach is quite cumbersome. Is there a more comfortable way?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source