有没有办法选择加入时哪些列会被滚动?我试图建立缺失的列,有点像这样:
dt <- structure(list(id = c(1L, 1L, 2L, 2L), c = c(1L, 2L, 1L, 2L),
r = c(1L, 1L, 1L, 3L), ir1 = c(0.690859583206475, 0.0255513035226613,
0.381394953466952, 0.646164963953197), ir2 = c(0.869432955048978,
0.911753778578714, 0.421218475792557, 0.653007542947307)), .Names = c("id",
"c", "r", "ir1", "ir2"), class = c("data.table", "data.frame"
), row.names = c(NA, -4L))
expander <- expand.grid(id = 1:2, c=1:2, r=1:5)
setkey(dt, id, c, r)
setkey(setDT(expander),id,c,r)
dt[expander, roll = TRUE]
现在让我们说我只想ir2
滚动,将NA值留在ir1
。那可能吗?
我试图避免在数百万行上使用na.locf()
来处理在API上运行的进程(每个毫秒计数)。
奖励:关于创建expander
对象的data.table方法的评论也会有所帮助(假设它更像是固定data.frame(id,c)
并且重复r
)