title: "Loop through" data.table to calculate conditional averages
body: <p>I want to "loop through" the rows of a data.table and calculate an average for each row. The average should be calculated based on the following mechanism:</p>
  
  <ol>
  <li>Look up the identifier ID in row i (ID(i))</li>
  <li>Look up the value of T2 in row i (T2(i))</li>
  <li>Calculate the average over the <code>Data1</code> values in all rows <code>j</code>, which meet these two criteria: <code>ID(j) = ID(i)</code> and <code>T1(j) = T2(i)</code></li>
  <li><p>Enter the calculated average in the column Data2 of row i</p>
  
  <pre><code> DF = data.frame(ID=rep(c("a","b"),each=6), 
               T1=rep(1:2,each=3), T2=c(1,2,3), Data1=c(1:12))
   DT = data.table(DF)
   DT[ , Data2:=NA_real_]
       ID T1 T2  Data1 Data2
  [1,]  a  1  1     1    NA
  [2,]  a  1  2     2    NA
  [3,]  a  1  3     3    NA
  [4,]  a  2  1     4    NA
  [5,]  a  2  2     5    NA
  [6,]  a  2  3     6    NA
  [7,]  b  1  1     7    NA
  [8,]  b  1  2     8    NA
  [9,]  b  1  3     9    NA
  [10,] b  2  1    10    NA
  [11,] b  2  2    11    NA
  [12,] b  2  3    12    NA
  </code></pre></li>
  </ol>
  
  <p>For this simple example the result should look like this:</p>
  
  <pre><code>      ID T1 T2  Data1 Data2
  [1,]  a  1  1     1    2
  [2,]  a  1  2     2    5
  [3,]  a  1  3     3    NA
  [4,]  a  2  1     4    2
  [5,]  a  2  2     5    5
  [6,]  a  2  3     6    NA
  [7,]  b  1  1     7    8
  [8,]  b  1  2     8    11
  [9,]  b  1  3     9    NA
  [10,] b  2  1    10    8
  [11,] b  2  2    11    11
  [12,] b  2  3    12    NA
  </code></pre>
  
  <p>I think one way of doing this would be to loop through the rows, but I think that is inefficient. I've had a look at the <code>apply()</code> function, but I'm sure if it would solve my problem. I could also use <code>data.frame</code> instead of <code>data.table</code> if this would make it much more efficient or much easier. The real dataset contains approximately 1 million rows.</p>