title: R: calculate number of distinct categories in the specified time frame
body: <p>here's some dummy data:</p>
  
  <pre><code>  user_id       date category
         27 2016-01-01    apple
         27 2016-01-03    apple
         27 2016-01-05     pear
         27 2016-01-07     plum
         27 2016-01-10    apple
         27 2016-01-14     pear
         27 2016-01-16     plum
         11 2016-01-01    apple
         11 2016-01-03     pear
         11 2016-01-05     pear
         11 2016-01-07     pear
         11 2016-01-10    apple
         11 2016-01-14    apple
         11 2016-01-16    apple
  </code></pre>
  
  <p>I'd like to calculate <strong>for each <code>user_id</code> the number of distinct <code>categories</code> in the specified time period (e.g. in the past 7, 14 days), including the current order</strong> </p>
  
  <p>The solution would look like this:</p>
  
  <pre><code> user_id       date category distinct_7 distinct_14
        27 2016-01-01    apple          1           1
        27 2016-01-03    apple          1           1
        27 2016-01-05     pear          2           2
        27 2016-01-07     plum          3           3
        27 2016-01-10    apple          3           3
        27 2016-01-14     pear          3           3
        27 2016-01-16     plum          3           3
        11 2016-01-01    apple          1           1
        11 2016-01-03     pear          2           2
        11 2016-01-05     pear          2           2
        11 2016-01-07     pear          2           2
        11 2016-01-10    apple          2           2
        11 2016-01-14    apple          2           2
        11 2016-01-16    apple          1           2
  </code></pre>
  
  <p>I posted similar questions <a href="https://stackoverflow.com/questions/41615967/r-calculate-the-number-of-occurrences-of-a-specific-event-in-the-past-and-futur">here</a> or <a href="https://stackoverflow.com/questions/41020670/r-calculate-cumulative-sums-and-counts-since-the-last-occurrence-of-a-value">here</a>, however none of it referred to counting cumulative unique values for the specified time period. Thanks a lot for your help!</p>