Fill in missing values by group in data.table

前端 未结 2 817

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: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]]
    

提交回复
热议问题