title: Cumulative sum with a threshold window in R data.table
body: <p>I want to calculate the rolling sum of n rows in my dataset where the window size 'n' depends on the sum itself. For example, I want to slide the window as soon as the rolling sum of time exceeds 5 mins. Basically, I want to calculate how much distance the person traveled in the last 5 mins but the time steps are not equally spaced. Here's a dummy data.table for clarity (the last two columns are required):</p>
  <p>I am looking for a data.table solution in R</p>
  <p>Input data table:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>Distance</th>
  <th>Time</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>2</td>
  <td>2</td>
  </tr>
  <tr>
  <td>1</td>
  <td>4</td>
  <td>1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2</td>
  <td>1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2</td>
  <td>2</td>
  </tr>
  <tr>
  <td>1</td>
  <td>3</td>
  <td>3</td>
  </tr>
  <tr>
  <td>1</td>
  <td>6</td>
  <td>3</td>
  </tr>
  <tr>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Desired Output:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>Distance</th>
  <th>Time</th>
  <th>5.min.rolling.distance</th>
  <th>5.min.rolling.time</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>2</td>
  <td>2</td>
  <td>NA</td>
  <td>NA</td>
  </tr>
  <tr>
  <td>1</td>
  <td>4</td>
  <td>1</td>
  <td>NA</td>
  <td>NA</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2</td>
  <td>1</td>
  <td>NA</td>
  <td>NA</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2</td>
  <td>2</td>
  <td>10</td>
  <td>6</td>
  </tr>
  <tr>
  <td>1</td>
  <td>3</td>
  <td>3</td>
  <td>5</td>
  <td>5</td>
  </tr>
  <tr>
  <td>1</td>
  <td>6</td>
  <td>3</td>
  <td>9</td>
  <td>6</td>
  </tr>
  <tr>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>10</td>
  <td>7</td>
  </tr>
  </tbody>
  </table>
  </div>