I have a dataset on malfunction times. The start of the malfunction is the Begin
column and the end is the End
column.
In addition there are 3 malfunction categories, which can also overlap in time.
My actual goal is to calculate the sum of the time period per day when there is no malfunction. (The category actually does not matter, I do not need a breakdown of the category).
For better illustration, here is an example data set of the malfunction times over 3 days:
> df_time
# A tibble: 8 × 3
Category Begin End
<chr> <dttm> <dttm>
1 A 2023-07-15 01:40:11 2023-07-15 13:43:15
2 A 2023-07-16 05:54:44 2023-07-16 10:50:45
3 B 2023-08-16 07:43:09 2023-08-16 16:42:12
4 C 2023-08-16 12:00:00 2023-08-16 13:11:13
5 A 2023-08-16 18:00:00 2023-08-16 19:30:00
6 A 2023-08-17 08:00:00 2023-08-17 13:00:00
7 C 2023-08-17 11:12:45 2023-08-17 19:58:22
8 A 2023-08-17 19:01:45 2023-08-17 23:59:59
I have now tried to visualize the malfunction times graphically:
Now I want to calculate a summary per calendar day, which shows how many minutes (or hours or seconds) no single malfunction occurred.
This is the sum of all green time intervals per day:
If there would be only one malfunction category, one could simply calculate the 24 hours minus the malfunction interval.
However, I do not get further in dealing with the different overlapping malfunction intervals.
Can anyone help me in calculating the malfunction free periods and/or also the daily malfunction periods.
I have imagined such an evaluation to be somewhat: (Where the results are probably not exactly the same as the example data set above)
Date OK_times malfunction_times
<date> <chr> <chr>
1 2023-07-15 17 hours 7 hours
2 2023-07-16 5 Hours 19 hours
3 2023-07-17 3 hours 21 hours
Here is the code for creating the example data frame:
library(tidyverse)
df_time <- tibble(
Category = c("A", "A", "B", "C", "A", "A", "C", "A"),
Begin = as_datetime(c(
"2023-07-15 01:40:11",
"2023-07-16 05:54:44",
"2023-08-16 07:43:09",
"2023-08-16 12:00:00",
"2023-08-16 18:00:00",
"2023-08-17 08:00:00",
"2023-08-17 11:12:45",
"2023-08-17 19:01:45"
)),
End = as_datetime(c(
"2023-07-15 13:43:15",
"2023-07-16 10:50:45",
"2023-08-16 16:42:12",
"2023-08-16 13:11:13",
"2023-08-16 19:30:00",
"2023-08-17 13:00:00",
"2023-08-17 19:58:22",
"2023-08-17 23:59:59"
))
)