Get the last row of a previous group in data.table
<p>This is what my data table looks like:</p>

<pre><code>library(data.table)
dt &lt;- fread('
    Product  Group    LastProductOfPriorGroup
    A          1          NA
    B          1          NA
    C          2          B
    D          2          B
    E          2          B
    F          3          E
    G          3          E
')
</code></pre>

<p>The <code>LastProductOfPriorGroup</code> column is my desired column. I am trying to fetch the product from last row of the prior group. So in the first two rows, there are no prior groups and therefore it is <code>NA</code>. In the third row, the product in the last row of the prior group 1 is <code>B</code>. I am trying to accomplish this by </p>

<pre><code>dt[,LastGroupProduct:= shift(Product,1), by=shift(Group,1)]
</code></pre>

<p>to no avail.</p>