Efficient way to Fill Time-Series per group

后端 未结 2 1231
天涯浪人
天涯浪人 2020-12-11 11:33

I was looking for a way to fill a time series data set by time, per group. The very very inefficient way I was using was to split the data set per group and app

2条回答
  •  自闭症患者
    2020-12-11 12:24

    It appears that data.table is really much faster than the tidyverse option. So merely translating the above into data.table(compliments of @Frank) completed the operation in little under 3 minutes.

    library(data.table)
    
    mDT = setDT(d1)[, .(grp = seq(min(grp), max(grp), by = "hour")), by = source]
    new_D <- d1[mDT, on = names(mDT)]
    
    new_D <- new_D[, cnt := replace(cnt, is.na(cnt), 0)] #If needed
    

提交回复
热议问题