I m trying to use the solution here: Replacing NAs with latest non-NA value
to a large data table, but can't manage to work out.
My datatable childs
is at the link here.
I want to create a column pop1
that takes value from pop
when value
is missing. The tricky bit is to pass the right group, which I am unable to.
if I subset first:
child_sub<- childs[ sector_number=='3.B.2.1.1' & meastype== 'NRATE']
and then
childs1 <- child_sub[, pop1:=pop[1], .( cumsum(!is.na(value)) )]
it works, in the sense that it gives pop1
for the year 2019 the value of pop
of 2018, that is 1591.251.
however, if I try:
childs1 <- childs[, pop1:=pop[1], .( party, sector_number, meastype, cumsum(!is.na(value)) )]
,
it doesn't assign the correct pop
(i.e.the pop
from the same meastype
, previous year), and I don't understant why...
I also tried with:
childs1 <- childs[is.na(value), pop1:=setnafill(pop, type = "locf")]
which throws error straight away.
any help appreciated