Hi,
I have a R script which creates Date & Hour Time bins to show how many Colleagues are working in any given hour, and also the first Colleague that arrives during that Hour Period.
What I now want to do, is drill down to minute variance.
Example below with values in ()
I need it such as: DateTime (2019-11-11 00:01), Number of Colleagues (2), First Colleague (45).
TimeSheet <- readxl::read_excel("C:/Users/Matt/Colleagues.xlsx")
TimeSheet <- TimeSheet %>% mutate(`Start Time (UTC)` = lubridate::ymd_hms(`Start Time (UTC)`, tz = "UTC"),
`Actual End Time (UTC)` = lubridate::ymd_hms(`Actual End Time (UTC)`, tz = "UTC"))
Seq1 <- seq.POSIXt(as.POSIXct("2018-11-12 00:00:00", tz = "UTC"),
as.POSIXct("2019-12-31 00:00:00", tz = "UTC"), "hour")
DF <- data.frame(DateTime = c(Seq1))
DF2 <- DF %>% group_by(DateTime) %>%
mutate(Colleague = sum(DateTime >= TimeSheet$`Start Time (UTC)` & DateTime < TimeSheet$`Actual End Time (UTC)`))
HourColleague <- TimeSheet %>% mutate(ID = 1:nrow(TimeSheet),
HourStart = hour(ceiling_date(`Start Time (UTC)`, unit = "hour")),
HourEnd = hour(floor_date(`Actual End Time (UTC)` - 60, unit = "hour"))) %>%
filter(HourStart <= HourEnd) %>%
group_by(ID) %>%
mutate(HourSeq = list(tidyr::full_seq(c(HourStart, HourEnd), 1))) %>%
tidyr::unnest(cols = HourSeq) %>%
mutate(DateTime_bin = make_datetime(year = year(`Start Time (UTC)`),
month = month(`Start Time (UTC)`),
day = day(`Start Time (UTC)`),
hour = HourSeq)) %>%
ungroup()
HourColleague <- select(HourColleague, DateTime = DateTime_bin, ColleagueNumber)
HourColleague
DF3 <- left_join(DF2, HourColleague)
Thanks for help,