1
$\begingroup$

I have a (large) dataset where I know, for each observation, the departure and the arrival time of a worker (between 0 and 24, in decimal) :

ID      Departure_time    Arrival_time
0001    07.00             08.25
0002    07.55             08.20
0003    08.10             09.75
...     ...               ...

I can plot the density of the departure_time (or arrival_time) easily :

ggplot(df, aes(x=Departure_time)) + geom_density(adjust=.5)

I would like to plot the density of workers traveling for a given time (for each moment, the number of worker who are not at work (prior to arrival_time) but who have left home (after departure_time).

$\endgroup$
  • $\begingroup$ Do you want to know the number of workers en route at a given point in time, or the typical duration of a worker's commute? $\endgroup$ – gung Jun 19 '13 at 14:17
  • $\begingroup$ The number of workers at a given time! I could get the typical duration with a subtraction: mean(arrival_time-departure_time). $\endgroup$ – Sylvain Jun 19 '13 at 14:20
  • $\begingroup$ A loop over workers and a counter wich is incremented if departure < time < arrival will give you the number of workers travelling at a given time... Then loop on times. $\endgroup$ – were_cat Jun 19 '13 at 15:07
1
$\begingroup$

Assuming your data is in a vector called data

#Make this as fine as you want it. 
times<-seq(1,24,by=0.01)

d<-sapply(times,function(x){
  sum(data[,2]<=x & data[,3]>=x)
 })

hist(d)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.