Fill in missing values by group in data.table
Here's a slightly faster and more compact way of doing it (version 1.9.3+):
DT[, filled4 := DT[!is.na(value)][DT, value, roll = T]]
There is now a native data.table
way of filling missing values (as of 1.12.4
).
This question spawned a github issue which was recently closed with the creation of functions nafill
and setnafill
. You can now use
DT[, value_filled_in := nafill(value, type = "locf")]
It is also possible to fill NA
with a constant value or next observation carried back.
One difference to the approach in the question is that these functions currently only work on NA
not NaN
whereas is.na
is TRUE
for NaN
- this is planned to be fixed in the next release through an extra argument.
I have no involvement with the project but I saw that although the github issue links here, there was no link the other way so I'm answering on behalf of future visitors.
Update: By default NaN
is now treated same as NA
.