title: R aggregate data on a daily basis if start and end times are available
body: <p>I have the following problem. I have a dataframe of the following strucutre:</p>
  
  <pre><code>        startdatetime         enddatetime  type amount
  1 2019-02-01 03:35:00 2019-02-03 06:35:00 prod1  1e+03
  2 2019-02-03 06:35:00 2019-02-05 09:35:00 prod1  5e+03
  3 2019-02-05 09:35:00 2019-02-06 01:35:00 prod2  3e+07
  4 2019-02-06 01:35:00 2019-02-06 03:35:00 prod1  1e+02
  </code></pre>
  
  <p>representing an amount produced in a certain time span (startdatetime and enddatetime). Now I want to aggregate these data on a daily basis. Lets ignore the incomplete day 2019-02-01 and start with 2019-02-02. First prod 1 was produced between 2019-02-01 03:35:00 and 2019-02-03 06:35:00 in total 1000 kg were produced. So for example, on 2019-02-02: <code>24/51*1000</code> = 470.58 of prod 1 were produced because <code>24h + 21h + 6h = 51h</code>.
  The solution I have so far is based on a for and a while loop, but I guess there is a faster solution based on the package "lubridate" or else which I did not find. 
  Any suggestion?
  Below my code</p>
  
  <pre><code>#create test data set
  mydata &lt;- data.frame(startdatetime=c(as.POSIXct("2019-02-01 03:35:00"), as.POSIXct("2019-02-03 06:35:00"),as.POSIXct("2019-02-05 09:35:00"),as.POSIXct("2019-02-06 01:35:00")),
                       enddatetime  =c(as.POSIXct("2019-02-03 06:35:00"), as.POSIXct("2019-02-05 09:35:00"),as.POSIXct("2019-02-06 01:35:00"),as.POSIXct("2019-02-06 03:35:00")),
                       type=c("prod1","prod1","prod2","prod1"),
                       amount=c(1000,5000,30000000,100)) 
  
  # take only full days into account and ignore the first and the last day
  minstartday = min(mydata$startdatetime)+24*60*60
  maxendday   = max(mydata$enddatetime)-24*60*60
  
  #create a day index
  timesindex &lt;- seq(from = as.Date(format(minstartday, format = "%Y/%m/%d")), 
                    to   = as.Date(format(maxendday, format = "%Y/%m/%d")), by = "day")
  
  # create an empty dataframe which will be filled with the production data for each day
  prodperday &lt;- data.frame(Date=as.Date(timesindex),
                           prod1=replicate(length(timesindex),0), 
                           prod2=replicate(length(timesindex),0), 
                           stringsAsFactors=FALSE) 
  
  # loop over all entries and separate them into produced fractions per day
  for (irow in 1:dim(mydata)[1]){
    timestart = mydata[irow,"startdatetime"]
    datestart = as.Date(format(timestart, format = "%Y/%m/%d"))
    timeend = timestart
    tota_run_time_in_h = (as.numeric((mydata[irow,"enddatetime"]-mydata[irow,"startdatetime"])))*24.
    while (timeend &lt; mydata[irow,"enddatetime"]){
      timeend = min (as.POSIXct(datestart, format = "%Y/%m/%d %H:%M:%S")+23*60*60-1,
                     mydata[irow,"enddatetime"])
      tdiff = as.numeric(timeend-timestart)
      fraction_prod = (tdiff/tota_run_time_in_h)*mydata[irow,"amount"]
      if (datestart %in% prodperday$Date){
        prodperday[prodperday$Date == datestart,as.character(mydata[irow,"type"])] = 
          prodperday[prodperday$Date == datestart,as.character(mydata[irow,"type"])] + fraction_prod
      }
  
      timestart = timeend+1
      datestart = as.Date(format(timestart, format = "%Y/%m/%d"))
      timeend = timestart
    }
  }
  </code></pre>
  
  <p>and the result:</p>
  
  <pre><code>        Date     prod1   prod2
  1 2019-02-02  470.5828       0
  2 2019-02-03 1836.5741       0
  3 2019-02-04 2352.9139       0
  4 2019-02-05  939.5425 1126280
  </code></pre>