Fill in missing values by group in data.table

前端 未结 2 800

If one wants to fill in missing values of a variable based on previous/posterior non NA observation within a group, the data.table command is

setkey(DT,id,da         


        
相关标签:
2条回答
  • 2020-11-27 19:25

    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.

    0 讨论(0)
  • 2020-11-27 19:26

    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]]
    
    0 讨论(0)
提交回复
热议问题