title: subset datatable to replace NAs with latest non-NA value
body: <p>I m trying to use the solution here:
  <a href="https://stackoverflow.com/questions/7735647/replacing-nas-with-latest-non-na-value">Replacing NAs with latest non-NA value</a></p>
  <p>to a large data table, but can't manage to work out.</p>
  <p>My datatable <code>childs</code> is at the link <a href="https://drive.google.com/file/d/17MwTpk0l9vX-A3gUXlPUoszLZ8OchdIb/view?usp=sharing" rel="nofollow noreferrer">here</a>.
  I want to create a column <code>pop1</code> that takes value from <code>pop</code> when <code>value</code> is missing. The tricky bit is to pass the right group, which I am unable to.</p>
  <p>if I subset first:</p>
  <pre><code>child_sub&lt;- childs[ sector_number=='3.B.2.1.1' &amp; meastype== 'NRATE']
  </code></pre>
  <p>and then</p>
  <pre><code>childs1 &lt;- child_sub[, pop1:=pop[1], .( cumsum(!is.na(value)) )]
  </code></pre>
  <p>it works, in the sense that it gives <code>pop1</code> for the year 2019 the value of <code>pop</code> of 2018, that is 1591.251.
  however, if I try:</p>
  <p><code>childs1 &lt;- childs[, pop1:=pop[1], .( party, sector_number, meastype, cumsum(!is.na(value)) )]</code>,
  it doesn't assign the correct <code>pop</code> (i.e.the <code>pop</code> from the same <code>meastype</code>, previous year), and I don't understant why...</p>
  <p>I also tried with:</p>
  <pre><code>childs1 &lt;- childs[is.na(value), pop1:=setnafill(pop, type = &quot;locf&quot;)]
  </code></pre>
  <p>which throws error straight away.</p>
  <p>any help appreciated</p>