Valentin Valentin - 1 month ago 5x
R Question

busy percentage for intervals in R

I work in a hospital. Our doctors work on-call during the night and evening hours. There may be times when no patients are coming so they can rest. At other times, many patients will be there at once.
They write down when they started and when they stopped treating a patient. With the lubridate package, I can transform these data into intervals with specific dates. The length of these intervals will vary a lot as treatment may be more or less complicated. Also when a lot is going on, a doctor may be going back and forth between patients. So a typical entry will look like this: "2016-06-11 21:45:00 UTC" "2016-06-11 22:35:00 UTC"

To see which times of day are usually very busy and which are rather slow, I would like to use these data. This should also be possible for different days of the week.
The whole thing should probably look like a bar graph showing what the average occupation would be at any time of day (for example 100% occupation between 8 and 9 pm, 40% between 1 and 2 am).
My problem is that I don't know how to do that. ggplot will not handle intervals and I have not found any package that will do this average or percentage for intervals.

I hope I was able to make clear what I need and what my problems are. I'm not an experienced programmer but happy to learn.

Thanks a lot



Sorry, I should have thought of that. So here is as far as I have come:

>Beginn<-parse_date_time(Daten$Beginn,"dmy HM“,tz="CET“)
>Ende<-parse_date_time(Daten$Ende,"dmy HM",tz="CET“)
##Interval with date information
##Intervals stripped of date
>Daten$Beg<-as.POSIXct(strftime(Beginn, format="%H:%M:%S"), format="%H:%M:%S")
> Daten$dur<-as.duration(Daten$Intervall)
> Daten$Interv<-as.interval(Daten$dur , Daten$Beg)
## add weekdays

This way I have time intervals pointing to the same date and I have the weekdays to sort the data by. This is where I am stuck, because I know of no way to some kind of histogram on intervals. I could just use starting dates, but that would be heavily skewed, because an interval might be between 5 mins and 2 hours.

I hope the code helps. If you need some exemplary data, just tell me.

EDIT (2):
These are the raw data

and the output from dput:

I am afraid the data are not as well structured as could be, but it should still work. Not sure if it is a good idea to post the output inline, so I provided the file.


So after digging around and trying different things, here is what i came up with and what works for me. The salution is somewhat convoluted and verbose bus apparently gives exact results. In trying to find the answer, I learned about the vonders of vectorisation (pardon my german accent) since producing vectorised code cut the time it took to compute the results to about 3 mins whereas before i stopped calculations after about 96 hours without having completed.

Please note that the list of documented dates (not every doctor will have completed documentation of his shift) is an excel sheet with simple dates. the list of documented times working intervals is a date and time someone started seeing a patient in one column and stopped seeing that patient in another. The next row will be similar start and stop time and date.

All the variables in the text are in german or are abbreviations of german words, but I hope my comments are sufficient to understand what's going on. Also, a lot of the code is for problems specific to my situation.

Special thanks to users PhiSeu and user3507085 who helped me with different aspects of the solution.

#read dates
#convert start dates to POSIX
Daten$Beginn<-parse_date_time(Daten$Beginn,"dmy HM",tz="CET")
#prevent overlap by adding one second
#convert end dates to POSIX
Daten$Ende<-parse_date_time(Daten$Ende,"dmy HM",tz="CET")
#remove empty rows
#create intervals in which people worked
#read dates on which people worked

#create a start time of 09 A.M. for shifts
#add column names
#convert to POSIX

#Loop to create 15 min intervals for each documented shift spanning 24 hour against which actual working hours will be checked

begin <- as.POSIXct(doku$Beginn)

# copy begin time for loop
begin_new <- begin

# create duration object
aufl <- duration(15, "mins")

# count times for loop
times <- 24*60/15

# create dataframe with begin time
Intervall <- data.frame(begin,stringsAsFactors = FALSE)

for (i in 1:times){


  # save old time for interval calculation
  begin_start <- begin_new
  # add 15 Minutes to original time
  begin_new <- begin_new + aufl


  # create an interval object between 
  new_dur <- interval(begin_start,begin_new)

  # bind to original dataframe
  Intervall <- cbind(Intervall,new_dur) 


# Add column names
vec_names <- paste0("v",c(1:(times+1)))
colnames(Intervall) <- vec_names

#create a matrix of the number of seconds worked in each of the above 15 intervals by checking the amount of intersection between 15 intervals and documented intervals of work

for (j in 1:length(doku$Beginn)){
for (k in 1:times){

#cadd start time to the above matrix
#convert to POSIX

##analysis of data
#common to all days of the week
#create labels for 15 min intervals

##analysis for weekends
#how many percent people worked on average in any of the 15 min intervals on a saturday or sunday
Prozent<-Wochenende/length(Ausw$begin[Ausw$wtag==c(1,7)]) /as.numeric(aufl)*100

#add labels
#plot as barplot and add axis labels
b=barplot(Prozent,axes = F,axisnames=F,main="Durchschnittliche Arbeitsbelastung am Wochenende",sub="über 100%: Übergabezeiten",xlab="Uhrzeit",ylab="Prozent")
axis(1,at=c(b[seq(1,length(Labels),4)],b[length(b)]+diff(b)[1]),labels = c(Labels[seq(1,length(Labels),4)],"09"))

##analysos monday to friday
Prozent2<-Woche/length(Ausw$begin[Ausw$wtag==c(2,3,4,5,6)]) /as.numeric(aufl)*100
#add labels
#plot as barplot and add axis labels
b2=barplot(Prozent2,axes = F,axisnames=F,main="Durchschnittliche Arbeitsbelastung Montag - Freitag",,xlab="Uhrzeit",ylab="Prozent“,ylim=c(0,100))
axis(1,at=c(b2[seq(1,length(Labels),4)],b2[length(b2)]+diff(b2)[1]),labels = c(Labels[seq(1,length(Labels),4)],"09"))