Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The secret of fast performing code in R is to keep everything in vector, or arrays, which are really just arrays in disguise.</p> <p>Here is a solution that makes use exclusively of base R arrays. Your sample of data is tiny so I use <code>replicate</code> and <code>system.time</code> combined to measure performance.</p> <p>My solution is roughly 6 times faster than your solution with <code>sapply</code> and <code>data.table</code>. (My solution takes 0.6 seconds to solve your small sample data set 1,000 times.)</p> <p>Timing your solution</p> <pre><code>system.time(replicate(1000, XX &lt;- data.frame( intervals, count = sapply(1:5, function(i) sum(events[, start &lt;= intervals[i] &amp; intervals[i] &lt;= finish]))) )) user system elapsed 4.04 0.05 4.11 </code></pre> <p>My solution. First create two helper functions to create equal sized arrays with events running down the columns and intervals running across the rows. Then do a simple vector comparison followed by <code>colSums</code>:</p> <pre><code>event.array &lt;- function(x, interval){ len &lt;- length(interval) matrix(rep(unclass(x), len), ncol=len) } intervals.array &lt;- function(x, intervals){ len &lt;- length(x) matrix(rep(unclass(intervals), len), nrow=len, byrow=TRUE) } a.start &lt;- event.array(start, intervals) a.finish &lt;- event.array(finish, intervals) a.intervals &lt;- intervals.array(start, intervals) data.frame(intervals, count=colSums(a.start &lt;= a.intervals &amp; a.finish &gt;= a.intervals)) intervals count 1 (01/02/09 09:30:00) 3 2 (01/02/09 09:45:00) 7 3 (01/02/09 10:00:00) 19 4 (01/02/09 10:15:00) 18 5 (01/02/09 10:30:00) 12 </code></pre> <p>Timing my solution</p> <pre><code>system.time(replicate(1000, YY &lt;- data.frame( intervals, count=colSums(a.start &lt;= a.intervals &amp; a.finish &gt;= a.intervals)) )) user system elapsed 0.67 0.02 0.69 all.equal(XX, YY) [1] TRUE </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload