R - time intervals of overlapping plot

I have a list of people and their working start and end time during the day. I want to build a curve showing the total number of people working any minute of the day. I could add only 1440 additional conditional logical variables for every minute of the day and summarize them, but this seems very inelegant. I am wondering if there is a better way to do this (integrals?).

Here is the code for generating df with my sample data:

sample_wt <- function() { require(lubridate) set.seed(10) worktime <- data.frame( ID = c(1:100), start = now()+abs(rnorm(100,4800,2400)) ) worktime$end <- worktime$start + abs(rnorm(100,20000,10000)) worktime$length <- difftime(worktime$end, worktime$start, units="mins") worktime } 

To create a sample data, you can do something like:

 DF <- sample_wt() 
+8
datetime r plot
source share
3 answers

Here is one option using the Bioconductor IRanges package.

 library(IRanges) ## generate sample DF <- sample_wt() ## create the range from the sample data rangesA <- IRanges(as.numeric(DF$start), as.numeric(DF$end)) ## create one minute range xx = seq(min(DF$start),max(DF$end),60) rangesB <- IRanges(as.numeric(xx),as.numeric(xx+60)) ## count the overlaps ov <- countOverlaps(rangesB, rangesA, type="within") ## plot the result plot(xx,ov,type='l') 

enter image description here

+5
source share

I don't have lubridate , so I created data.frame via Sys.time instead of now (suppose they should be similar). This can do the trick:

  minutes<-seq(as.POSIXct(paste(sep="",Sys.Date()," 00:00:00")),by="min",length.out=24*60) rowSums(outer(minutes,worktime$start,">") & outer(minutes,worktime$end,"<")) 
+1
source share

Of course, it can be improved, but it looks like this:

 time_range <- seq(min(DF$start), max(DF$end), 60) result <- integer(length(time_range)) for (t in seq_along(time_range)) { result[t] <- sum(DF$start <= time_range[t] & DF$end >= time_range[t]) } 
0
source share

All Articles