Hi FJCC.
Firstly apologies , relatively new to this forum.
The below code is perfect, it works great. Now I want to look at it by minute. So I want to see the number of colleagues by minute, and the first colleague by that minute.
library(dplyr)
TimeSheet <- tibble::tribble(
~Colleague.Number, ~Start.Time, ~Actual.End.Time,
45, "17/11/2019 08:40", "17/11/2019 09:27",
42, "17/11/2019 08:15", "17/11/2019 08:40",
32, "17/11/2019 07:20", "17/11/2019 08:15",
35, "17/11/2019 05:00", "17/11/2019 07:20",
42, "14/11/2019 05:40", "14/11/2019 07:16",
42, "12/11/2019 12:20", "12/11/2019 12:54",
40, "12/11/2019 10:00", "12/11/2019 12:20",
42, "12/11/2019 07:20", "12/11/2019 10:00"
)
TimeSheet <- TimeSheet %>% mutate(Start.Time = lubridate::dmy_hm(Start.Time, tz = "UTC"),
Actual.End.Time = lubridate::dmy_hm(Actual.End.Time, tz = "UTC"))
Seq1 <- seq.POSIXt(as.POSIXct("2019-11-17 05:00:00", tz = "UTC"),
as.POSIXct("2019-11-17 10:00:00", tz = "UTC"), "h")
Seq2 <- seq.POSIXt(as.POSIXct("2019-11-14 05:00:00", tz = "UTC"),
as.POSIXct("2019-11-14 08:00:00", tz = "UTC"), "h")
Seq3 <- seq.POSIXt(as.POSIXct("2019-11-12 07:00:00", tz = "UTC"),
as.POSIXct("2019-11-12 13:00:00", tz = "UTC"), "h")
DF <- data.frame(DateTime = c(Seq1, Seq2, Seq3))
DF2 <- DF %>% group_by(DateTime) %>%
mutate(CollAtWork = sum(DateTime >= TimeSheet$Start.Time & DateTime < TimeSheet$Actual.End.Time))
library(lubridate)
HourColleague <- TimeSheet %>% mutate(ID = 1:nrow(TimeSheet),
HourStart = hour(ceiling_date(Start.Time, unit = "hour")),
HourEnd = hour(floor_date(Actual.End.Time - 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),
month = month(Start.Time),
day = day(Start.Time),
hour = HourSeq)) %>%
ungroup()
HourColleague <- select(HourColleague, DateTime = DateTime_bin, Colleague.Number)
HourColleague
DF3 <- left_join(DF2, HourColleague)
#> Joining, by = "DateTime"
DF3
Is this possible?