我在数据表中缺少一行,用于描述time
,sid
和s.c
到count
的功能:
> dates.dt[1001:1011]
sid s.c count time
1: missing CLICK 104192 2013-05-25 10:00:00
2: missing SHARE 7694 2013-05-25 10:00:00
3: present CLICK 99573 2013-05-25 10:00:00
4: present SHARE 89302 2013-05-25 10:00:00
5: missing CLICK 28 2013-05-25 11:00:00
6: present CLICK 25 2013-05-25 11:00:00
7: present SHARE 15 2013-05-25 11:00:00
8: missing CLICK 104544 2013-05-25 12:00:00
9: missing SHARE 7253 2013-05-25 12:00:00
10: present CLICK 105891 2013-05-25 12:00:00
11: present SHARE 88709 2013-05-25 12:00:00
缺少的行是(我希望第1列和第2列以及每个时间片的两个值都有一行):
missing SHARE 0 2013-05-25 11:00:00
如何检测和恢复丢失的行?
我发现这个的方式是
library(data.table)
total <- dates.dt[, list(sum(count)) , keyby="time"]
setnames(total,"V1","total")
ts <- dates.dt[s.c=="SHARE" & sid=="missing", list(sum(count)) , keyby="time"]
cat("SHARE/missing:",nrow(ts),"rows\n")
stopifnot(identical(total$time,ts$time)) # --> ERROR!
total$shares.missing <- ts$V1
现在,我想我可以找到ts$time
和total$time
的第一个位置
不同并在那里插入0行,但这似乎相当乏味
过程
谢谢!
答案 0 :(得分:2)
按照@ Frank的建议你可以这样做:
setkey(dt, time, sid, s.c)
dt[J(expand.grid(unique(time),unique(sid),unique(s.c)))][order(time, sid, s.c)]
# time sid s.c count
# 1: 2013-05-25 10:00:00 missing CLICK 104192
# 2: 2013-05-25 10:00:00 missing SHARE 7694
# 3: 2013-05-25 10:00:00 present CLICK 99573
# 4: 2013-05-25 10:00:00 present SHARE 89302
# 5: 2013-05-25 11:00:00 missing CLICK 28
# 6: 2013-05-25 11:00:00 missing SHARE NA
# 7: 2013-05-25 11:00:00 present CLICK 25
# 8: 2013-05-25 11:00:00 present SHARE 15
# 9: 2013-05-25 12:00:00 missing CLICK 104544
#10: 2013-05-25 12:00:00 missing SHARE 7253
#11: 2013-05-25 12:00:00 present CLICK 105891
#12: 2013-05-25 12:00:00 present SHARE 88709