I have a dataframe with working times of every weekday. I have start and end times for each day. The problem arises when the employee worked twice on the same day. He started to work for example on Friday 10pm and ended on Saturday 8am but started again working at 10pm on Saturday evening. My dataset is now structured like this :
dput(droplevels(head(data1, 10)))
structure(list(Mon.Start1 = structure(c(2L, 1L, 1L, 4L, 1L, 3L,
2L, 4L, 1L, 1L), .Label = c(" ", "14:30:00", "22:30:00", "7:00:00"
), class = "factor"), Mon.End1 = structure(c(4L, 1L, 1L, 2L,
1L, 6L, 5L, 3L, 1L, 1L), .Label = c(" ", "16:00:00", "17:30:00",
"23:15:00", "23:30:00", "24:00:00"), class = "factor"), Mon.Start2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = " ", class = "factor"),
Mon.End2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = " ", class = "factor"), Tue.Start1 = structure(c(4L,
1L, 3L, 5L, 1L, 2L, 1L, 5L, 5L, 1L), .Label = c(" ", "0:00:00",
"14:00:00", "14:30:00", "7:00:00"), class = "factor"), Tue.End1 = structure(c(3L,
1L, 4L, 2L, 1L, 5L, 1L, 2L, 2L, 1L), .Label = c(" ", "16:30:00",
"23:15:00", "24:00:00", "8:00:00"), class = "factor"), Tue.Start2 = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c(" ", "22:30:00"
), class = "factor"), Tue.End2 = structure(c(1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c(" ", "24:00:00"), class = "factor"),
Wed.Start1 = structure(c(1L, 3L, 1L, 3L, 1L, 2L, 1L, 3L,
3L, 1L), .Label = c(" ", "0:00:00", "7:00:00"), class = "factor"),
Wed.End1 = structure(c(1L, 3L, 1L, 3L, 1L, 5L, 1L, 4L, 2L,
1L), .Label = c(" ", "16:30:00", "17:00:00", "17:30:00",
"8:00:00"), class = "factor"), Wed.Start2 = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c(" ", "22:30:00"
), class = "factor"), Wed.End2 = structure(c(1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c(" ", "24:00:00"), class = "factor"),
Thu.Start1 = structure(c(1L, 1L, 1L, 4L, 1L, 2L, 3L, 4L,
4L, 1L), .Label = c(" ", "0:00:00", "14:30:00", "7:00:00"
), class = "factor"), Thu.End1 = structure(c(1L, 1L, 1L,
2L, 1L, 4L, 3L, 2L, 2L, 1L), .Label = c(" ", "16:30:00",
"24:00:00", "8:00:00"), class = "factor"), Thu.Start2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = " ", class = "factor"),
Thu.End2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = " ", class = "factor"), Fri.Start1 = structure(c(2L,
1L, 1L, 4L, 3L, 1L, 1L, 4L, 4L, 1L), .Label = c(" ", "6:30:00",
"6:45:00", "7:00:00"), class = "factor"), Fri.End1 = structure(c(5L,
1L, 1L, 3L, 2L, 1L, 1L, 4L, 2L, 1L), .Label = c(" ", "16:30:00",
"16:45:00", "17:30:00", "18:30:00"), class = "factor"), Fri.Start2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = " ", class = "factor"),
Fri.End2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = " ", class = "factor"), Sa.Start1 = structure(c(4L,
5L, 1L, 1L, 3L, 1L, 1L, 5L, 1L, 2L), .Label = c(" ", "14:00:00",
"14:45:00", "6:30:00", "7:00:00"), class = "factor"), Sa.End1 = structure(c(4L,
3L, 1L, 1L, 5L, 1L, 1L, 2L, 1L, 6L), .Label = c(" ", "16:30:00",
"17:00:00", "18:30:00", "23:15:00", "24:00:00"), class = "factor"),
Sa.Start2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = " ", class = "factor"), Sa.End2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = " ", class = "factor"),
Sun.Start1 = structure(c(4L, 1L, 1L, 1L, 3L, 1L, 1L, 1L,
1L, 2L), .Label = c(" ", "0:00:00", "14:45:00", "6:30:00"
), class = "factor"), Sun.End1 = structure(c(3L, 1L, 1L,
1L, 4L, 1L, 1L, 1L, 1L, 2L), .Label = c(" ", "0:30:00", "16:45:00",
"23:15:00"), class = "factor"), Sun.Start2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c(" ", "14:00:00"
), class = "factor"), Sun.End2 = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c(" ", "24:00:00"), class = "factor")), row.names = c("21",
"22", "23", "25", "26", "27", "28", "31", "34", "35"), class = "data.frame")
I tried to set start and ending times with the package lubridate.I added also dates to all days (as.POSICxt) When the employee just works one time in a day it works but not when he worked twice. Since sometimes my End time is really the End time (as for Monday 24:00 is the end time) but sometimes it continues overnight(as Friday and Saturday) and so the 24:00 is not the end time . Is there a function to set that everytime that the start time is 00 the start time should be the start2 from the day before? Or how could I solve this problem? The goal would be to create breaks and label every shift, by the starting times as night, late shift etc and the difftime. Also for those who worked twice if I cut and label the beginning hours to define the shift, since the employees have two beginning hours I end with one more shift per week that in fact is not correct.
The desired output would be to have the day of the week in one column, start time, end time, shift length, shift type(off, morning, late, night) and then after having this for every day be able to make some calculations at the weekly level and further on the monthly level.
If someone could give me some advices I would be very thankful. Thanks