title: Does data table guarantee the same order after group by calculation?
body: <p>I have a <a href="/questions/tagged/data.table" class="post-tag" title="show questions tagged &#39;data.table&#39;" rel="tag">data.table</a>.</p>
  
  
  
  <pre class="lang-r prettyprint-override"><code># Load package
  library(data.table)
  
  # Set RNG seed
  set.seed(42)
  
  # Create dummy data
  dt &lt;- data.table(alpha = sample(letters[1:3], 27, replace = TRUE), beta = 1:27)
  
  # Peek
  dt
  #&gt;     alpha beta
  #&gt;  1:     a    1
  #&gt;  2:     a    2
  #&gt;  3:     a    3
  #&gt;  4:     a    4
  #&gt;  5:     b    5
  #&gt;  6:     b    6
  #&gt;  7:     b    7
  #&gt;  8:     a    8
  #&gt;  9:     c    9
  #&gt; 10:     c   10
  #&gt; 11:     a   11
  #&gt; 12:     a   12
  #&gt; 13:     b   13
  #&gt; 14:     b   14
  #&gt; 15:     b   15
  #&gt; 16:     c   16
  #&gt; 17:     c   17
  #&gt; 18:     a   18
  #&gt; 19:     a   19
  #&gt; 20:     c   20
  #&gt; 21:     a   21
  #&gt; 22:     c   22
  #&gt; 23:     a   23
  #&gt; 24:     a   24
  #&gt; 25:     b   25
  #&gt; 26:     c   26
  #&gt; 27:     b   27
  #&gt;     alpha beta
  </code></pre>
  
  <p>Now, I want to perform a group by calculation.</p>
  
  <pre class="lang-r prettyprint-override"><code># Perform calculation by group
  dt[, gamma := beta * .N, by = "alpha"]
  
  # Peek
  dt
  #&gt;     alpha beta gamma
  #&gt;  1:     a    1    12
  #&gt;  2:     a    2    24
  #&gt;  3:     a    3    36
  #&gt;  4:     a    4    48
  #&gt;  5:     b    5    40
  #&gt;  6:     b    6    48
  #&gt;  7:     b    7    56
  #&gt;  8:     a    8    96
  #&gt;  9:     c    9    63
  #&gt; 10:     c   10    70
  #&gt; 11:     a   11   132
  #&gt; 12:     a   12   144
  #&gt; 13:     b   13   104
  #&gt; 14:     b   14   112
  #&gt; 15:     b   15   120
  #&gt; 16:     c   16   112
  #&gt; 17:     c   17   119
  #&gt; 18:     a   18   216
  #&gt; 19:     a   19   228
  #&gt; 20:     c   20   140
  #&gt; 21:     a   21   252
  #&gt; 22:     c   22   154
  #&gt; 23:     a   23   276
  #&gt; 24:     a   24   288
  #&gt; 25:     b   25   200
  #&gt; 26:     c   26   182
  #&gt; 27:     b   27   216
  #&gt;     alpha beta gamma
  </code></pre>
  
  <p><sup>Created on 2020-03-05 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>
  
  <p>The resulting data table is in the same order as the original data table. </p>
  
  <p>My question: is this behaviour <strong>guaranteed</strong> or should I always reorder after a group by calculation to ensure the same order as before the calculation?</p>