title: Summarize values for overlapping time periods
body: <p>I'm trying to summarize values for overlapping time periods.
  I can use only tidyr, ggplot2 and dplyr libraries. Base R is preferred though.</p>
  
  <p>My data looks like this, but usually it has around 100 records:</p>
  
  <pre><code>df &lt;- structure(list(Start = structure(c(1546531200, 1546531200, 546531200, 1546638252.6316, 1546549800, 1546534800, 1546545600, 1546531200, 1546633120, 1547065942.1053), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Stop = structure(c(1546770243.1579, 1546607400, 1547110800, 1546670652.6316, 1547122863.1579, 1546638252.6316, 1546878293.5579, 1546416000, 1546849694.4, 1547186400), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Value = c(12610, 520, 1500, 90, 331380, 27300, 6072, 4200, 61488, 64372)), .Names = c("Start", "Stop", "Value"), row.names = c(41L, 55L, 25L, 29L, 38L, 28L, 1L, 20L, 14L, 31L), class = c("tbl_df", "tbl", "data.frame"))
  </code></pre>
  
  <p><code>head(df)</code> and <code>str(df)</code> gives:</p>
  
  <pre><code>          Start                Stop      Value
  2019-01-03 16:00:00 2019-01-06 10:24:03  12610
  2019-01-03 16:00:00 2019-01-04 13:10:00    520
  2019-01-03 16:00:00 2019-01-10 09:00:00   1500
  2019-01-04 21:44:12 2019-01-05 06:44:12     90
  2019-01-03 21:10:00 2019-01-10 12:21:03 331380
  2019-01-03 17:00:00 2019-01-04 21:44:12  27300
  
  Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   10 obs. of  3 variables:
   $ Start: POSIXct, format: "2019-01-03 16:00:00" "2019-01-03 16:00:00" ...
   $ Stop : POSIXct, format: "2019-01-06 10:24:03" "2019-01-04 13:10:00" ...
   $ Value: num  12610 520 1500 90 331380 ...
  </code></pre>
  
  <p>So there are overlapping time periods with "Start" and "Stop" dates with assigned value. In any given record when there is a value between <code>df$Start</code> and <code>df$Stop</code> and outside of this scope the value is 0.</p>
  
  <p>I want to create another dataframe based on which I could show how this values summarize and change over time. The Desired output would look like this (the "sum" column is made up):</p>
  
  <pre><code>&gt; head(df2)
                timestamp     sum
  "2019-01-02 09:00:00 CET"   14352
  "2019-01-03 17:00:00 CET"   6253
  "2019-01-03 18:00:00 CET"   23465
  "2019-01-03 21:00:00 CET"   3241
  "2019-01-03 22:10:00 CET"   23235
  "2019-01-04 14:10:00 CET"   123321
  </code></pre>
  
  <p>To get unique timestamps:</p>
  
  <pre><code>timestamps &lt;- sort(unique(c(df$`Start`, df$`Stop`)))
  </code></pre>
  
  <p>With <code>df2</code> dataframe I could easily draw a graph with ggplot, but how to get this sums?</p>
  
  <p>I think I should iterate over <code>df</code> data frame either some custom function or any built-it summarize function which would work like this:</p>
  
  <pre><code>fnct &lt;- function(date, min, max, value) {
    if (date &gt;= min &amp;&amp; date &lt;=max) {
      a &lt;- value
    }
    else {
    a &lt;- 0
    }
    return(a)
  }
  </code></pre>
  
  <p>...for every given <code>date</code> from <code>timestamps</code> iterate through <code>df</code> and give me a sum of values for the <code>timestamp</code>.</p>
  
  <p>It looks really simple and I'm missing something very basic.</p>