I have a data set that has an arbitrary 5 minute interval period represented in the Period
column. Periods for different observations (IDs
) start at different times (for ID='83' it started at 49M while for ID='90' it started at 50M). The observations are time intervals which are stored in Start.N
and End.N
columns.
These last 2 columns have one interval that represent the continuous time of observation or 'events' (regardless of period), hence it is repeated. Some observations have more than one 'event' and the Period are repeated to fit those extra intervals
My goal is to calculate the amount of minutes and seconds of overlap between the event(s) and the arbitrary bins. To clarify, The first row should have overlap 0M 0S
while the second row should have 5M 0S
overlap because 54M 0S - 59M 0S
is contained within 54M 1S - 89M 0S
.
ID Period Period.start Period.end Start.N End.N
1 83 5 49M 0S 54M 0S 54M 1S 89M 0S
2 83 10 54M 0S 59M 0S 54M 1S 89M 0S
3 83 15 59M 0S 64M 0S 54M 1S 89M 0S
4 83 20 64M 0S 69M 0S 54M 1S 89M 0S
5 83 25 69M 0S 74M 0S 54M 1S 89M 0S
6 83 30 74M 0S 79M 0S 54M 1S 89M 0S
Here's my data
structure(list(ID = c("83", "83", "83", "83", "83", "83", "83",
"83", "83", "83", "90", "90", "90", "90", "90", "90", "90", "90",
"90", "90", "90", "90", "90", "90", "90", "90", "90", "90", "90",
"90"), Period = c(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 5, 5,
10, 10, 15, 15, 20, 20, 25, 25, 30, 30, 35, 35, 40, 40, 45, 45,
50, 50), Period.start = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), year = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), month = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hour = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), minute = c(49, 54, 59, 64, 69, 74, 79,
84, 89, 94, 50, 50, 55, 55, 60, 60, 65, 65, 70, 70, 75, 75, 80,
80, 85, 85, 90, 90, 95, 95), class = structure("Period", package = "lubridate")),
Period.end = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), year = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), month = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hour = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), minute = c(54, 59, 64, 69,
74, 79, 84, 89, 94, 99, 55, 55, 60, 60, 65, 65, 70, 70, 75,
75, 80, 80, 85, 85, 90, 90, 95, 95, 100, 100), class = structure("Period", package = "lubridate")),
Start.N = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32), year = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), month = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), day = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), minute = c(54,
54, 54, 54, 54, 54, 54, 54, 54, 54, 52, 94, 52, 94, 52, 94,
52, 94, 52, 94, 52, 94, 52, 94, 52, 94, 52, 94, 52, 94), class = structure("Period", package = "lubridate")),
End.N = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), year = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), month = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), day = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), minute = c(89,
89, 89, 89, 89, 89, 89, 89, 89, 89, 83, 111, 83, 111, 83,
111, 83, 111, 83, 111, 83, 111, 83, 111, 83, 111, 83, 111,
83, 111), class = structure("Period", package = "lubridate"))), .Names = c("ID",
"Period", "Period.start", "Period.end", "Start.N", "End.N"), row.names = c(NA,
30L), class = "data.frame")