title: Iterate through DataTable to build a model for each ID
body: <p>I have a datatable that looks like this:</p>
  
  <pre><code>ID   Weeks  V1   V2   Sales
  1      1    100  200    30
  1      2    100  210    40
  1      3    102  240    50
  2      1    120  220    35
  3      1    110  202    51
  ...
  </code></pre>
  
  <p>My aim is to predict sales in week 9 for every ID. Most IDs are filled with values from week 1-8, others just have one value.  This is basically what I want for every ID and not just ID 26:</p>
  
  <pre><code>train_ID26 = train[ID==26,]
  test_ID26 = test[ID==26,]
  model_ID26 = glm(Sales~ V1+V2,data = train_ID26)
  test_ID26[,pred:= predict(model_ID26, newdata=test_ID26, type="response")]
  </code></pre>
  
  <p>I think this could be done with a for loop, but unfortunately, I am not familiar with those in R and couldn't figure out to build one. I also read that loops are often not the preferred choice in R.</p>