title: R data.table find new encountered level within time period
body: <h2>Background</h2>
  
  <p>I have a dataset of customers, their associated purchases and date of the purchase. I need to find a "next product". A next product is defined as a product that the customer hasn't bought in the previous 24 months and the next product will then be the current product. Notice that the name "next product" doesn't imply that I'm trying to predict anything. I just want to check if the customer has bought this particular product within the previous 24 months and if not, the next product is the current product.</p>
  
  <h2>Data</h2>
  
  <p>Below is a minimal working example:</p>
  
  <pre><code>&gt; test.dt
      ID   Product Diff_date
   1:  1 Product_C    0 days
   2:  1 Product_A   91 days
   3:  1 Product_A  122 days
   4:  1 Product_A  700 days
   5:  1 Product_A  700 days
   6:  1 Product_A  700 days
   7:  1 Product_A  731 days
   8:  1 Product_A  731 days
   9:  1 Product_C  761 days
  10:  1 Product_A  761 days
  11:  2 Product_A   30 days
  12:  2 Product_B   60 days
  13:  2 Product_C   91 days
  </code></pre>
  
  <p>And a data.table with the desired outcome, manually produced:</p>
  
  <pre><code>&gt; test.dt.outcome
      ID   Product Diff_date Next_product
   1:  1 Product_C    0 days         None
   2:  1 Product_A   91 days         None
   3:  1 Product_A  122 days         None
   4:  1 Product_A  700 days         None
   5:  1 Product_A  700 days         None
   6:  1 Product_A  700 days         None
   7:  1 Product_A  731 days         None
   8:  1 Product_A  731 days         None
   9:  1 Product_C  761 days    Product_C
  10:  1 Product_A  761 days         None
  11:  2 Product_A   30 days         None
  12:  2 Product_B   60 days         None
  13:  2 Product_C   91 days         None
  </code></pre>
  
  <p>As we can see customer with ID = 1 has bought Product_C previously but not within the last 24*30 = 720 days, so the next product is then Product C. On the other we have customer with ID = 2 that has bought different products, but within the time period of 24 months, so we do not define a new product.</p>
  
  <h2>Solution</h2>
  
  <p>I'm looking for a solution using the data.table package, but other approaches are also welcome.</p>
  
  <h2>Dputs</h2>
  
  <pre><code>library(data.table)
  test.dt &lt;- setDT(structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  2L, 2L, 2L), Product = c("Product_A", "Product_A", "Product_A", 
  "Product_A", "Product_A", "Product_A", "Product_A", "Product_A", 
  "Product_C", "Product_A", "Product_A", "Product_B", "Product_C"
  ), Diff_date = structure(c(0, 91, 122, 700, 700, 700, 731, 731, 
  761, 761, 30, 60, 91), units = "days", class = "difftime")), .Names = c("ID", 
  "Product", "Diff_date"), row.names = c(NA, -13L), class = c("data.table", 
  "data.frame")))
  
  
  test.dt.out &lt;- setDT(structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  2L, 2L, 2L), Product = c("Product_A", "Product_A", "Product_A", 
  "Product_A", "Product_A", "Product_A", "Product_A", "Product_A", 
  "Product_C", "Product_A", "Product_A", "Product_B", "Product_C"
  ), Diff_date = structure(c(0, 91, 122, 700, 700, 700, 731, 731, 
  761, 761, 30, 60, 91), units = "days", class = "difftime"), Next_product = c("Product_C", 
  "None", "None", "None", "None", "None", "None", "None", "Product_C", 
  "None", "None", "None", "None")), .Names = c("ID", "Product", 
  "Diff_date", "Next_product"), row.names = c(NA, -13L), class = c("data.table", 
  "data.frame")))
  </code></pre>