I have these four vectors with numbers being times in seconds:
maxtime = 240.0333
mintime = 181.1333
times1 = c(179.1333, 183.8000, 192.3000, 194.0000, 196.2500, 198.8333, 203.4333, 217.8167)
times2 = c(183.1333, 187.8000, 196.3000, 198.0000, 200.2500, 202.8333, 207.4333, 221.8167)
You will notice that times1
and times2
have the same length. Each corresponding element is 4 seconds apart. That is, times1
is 4 seconds earlier than times2
.
The best way to illustrate my questions is to plot these data like this:
library(ggplot2)
library(dplyr)
dfplot<- data.frame(ymin=times1, ymax=times2)
data.frame(x=c(rep("min",length(times1)), rep("max",length(times1))),
y=c(times1,times2),
id=1:length(times1)) %>%
ggplot(., aes(id,y,group=id)) +
geom_path(lwd=2) +
coord_flip() +
geom_hline(yintercept = as.numeric(mintime), lty=2,color='red', lwd=1)+
geom_hline(yintercept = as.numeric(maxtime), lty=2,color='red', lwd=1)+
geom_rect(data=dfplot,aes(xmin=0,ymin=ymin,xmax=length(times1),ymax=ymax,fill="red"),alpha=0.2,inherit.aes=FALSE) +
theme(panel.background = element_blank(), plot.background = element_blank())
What I want to do is to calculate the time that is covered by the intervals between each pair of elements in times1
and times2
. These are represented by the black horizontal lines and also by the red rectangles. As you can see, several of these might overlap. Effectively, I want to calculate what proportion of the time between the two red dashed lines is covered by the black lines/red rectangles and what proportion is not (i.e. the white gaps).
I hope this makes sense.