I have some code to take unit log ons and log offs by hour and group them so that I can see how many units are logged in by hour and division.
Code is as follows:
rm(list = ls())
library(dplyr)
library(lubridate)
library(openxlsx)
library(reshape2)
library(readxl)
library(tidyr)
library("tidyverse")
setwd("X:/_IPD Workload/Workload/02-Projects/01. Active Projects/P102 - MPP/03 Execute/Data")
start_date<-as.Date("2022-02-01") #Setting up dates to filter by later
end_date<-as.Date("2023-02-01")
df<-read_csv("Unit_Logon All Units.csv") #Read file
df2<- filter(df, Workload_Minutes >= 5) # Filter where log in time is greater than 5 minutes
df3<-df2[,-c(3:6,9)] #Get rid of unnecessary columns
names(df3)[names(df3) == "Unit_ID"]<-"ID"
names(df3)[names(df3) == "Log_On_Date_Time"]<-"Start"
names(df3)[names(df3) == "Log_Off_Date_Time"]<-"End"
names(df3)[names(df3) == "Unit_Dispatch_Group"]<-"Division"
df3$Start <- as.POSIXct(df3$Start, format = "%m/%d/%Y %H:%M")
df3$End <- as.POSIXct(df3$End, format = "%m/%d/%Y %H:%M")
df4<-df3 %>%
mutate(Start_hour=as.POSIXct(trunc(Start, units="hours")), #truncate start time to hour
End_hour=as.POSIXct(trunc(End, units="hours")), #truncate end time to hour
End_hour=case_when(End-End_hour>5~End_hour, #5-min rule
T~End_hour-3600)) %>%
rowwise() %>%
do(data.frame(Division=.$Division, ID=.$ID, time=seq(.$Start_hour, .$End_hour, by="1 hour"))) %>% #get rolling sequence
group_by(Division, time) %>%
summarise(n=n_distinct(ID)) #count distinct ID
df5 <- df4 %>% filter(`time` >= start_date &
`time` < end_date) #Date filter as per dates above
write.xlsx(df5, "Unitsbyhour.xlsx") #Write file
Data looks as below:
I would like to get this data into the following format
As you can see, I need it in 15 minute intervals, as well as by day of week, and I want to sum results by specified time intervals.
I am having trouble with this and requesting the communities help
Thank you
as above. It details what I have done. And where I am going