data.table equivalent of tidyr::complete()

后端 未结 2 2036
不知归路
不知归路 2020-11-30 10:00

tidyr::complete() adds rows to a data.frame for combinations of column values that are missing from the data. Example:

library(dply         


        
2条回答
  •  迷失自我
    2020-11-30 10:15

    There might be a better answer out there, but this works:

    dt[CJ(person=unique(dt$person), 
          observation_id=unique(dt$observation_id)),
       on=c('person','observation_id')]
    

    Which gives:

       person observation_id value
    1:      1              1     1
    2:      2              1     1
    3:      1              2    NA
    4:      2              2     1
    

    Now, if you would like to be able to fill with any value (and not NA), I would suggest to wait for the corresponding feature to be finished or contribute to it :)

提交回复
热议问题