1

当我尝试对我的 data.table 进行子集化时,我正在丢失数据。

这是写入文件的 .csv

Timestamp,Date,Time,SN,A.Ms.Amp,A.Ms.Vol,A.Ms.Watt,Pac
2013-10-01 12:00:00,2013-10-01,12:00:00,2110000001,23.04,465.43,10723,13544.5
2013-10-01 12:00:00,2013-10-01,12:00:00,2110000002,7.81,474.16,3704,6860
2013-10-01 12:00:00,2013-10-01,12:00:00,2110000003,6.97,484.19,3374,6661
2013-10-01 12:05:00,2013-10-01,12:05:00,2110000001,23.19,467.05,10830,13576
2013-10-01 12:05:00,2013-10-01,12:05:00,2110000002,8.4,462.52,3883.5,7366.5
2013-10-01 12:05:00,2013-10-01,12:05:00,2110000003,7.72,470.6,3631,7169
2013-10-01 12:10:00,2013-10-01,12:10:00,2110000001,23.98,470.29,11278.5,14127.5
2013-10-01 12:10:00,2013-10-01,12:10:00,2110000002,8.62,458.47,3952,7475.5
2013-10-01 12:10:00,2013-10-01,12:10:00,2110000003,7.9,462.62,3654,7182.33
2013-10-01 12:15:00,2013-10-01,12:15:00,2110000001,24.27,467.37,11342,14193
2013-10-01 12:15:00,2013-10-01,12:15:00,2110000002,8.61,458.96,3949,7502
2013-10-01 12:15:00,2013-10-01,12:15:00,2110000003,8.13,458.31,3725,7338
2013-10-01 12:20:00,2013-10-01,12:20:00,2110000001,22.3,461.71,10279.5,12735.5
2013-10-01 12:20:00,2013-10-01,12:20:00,2110000002,8.51,461.87,3929,7553.5
2013-10-01 12:20:00,2013-10-01,12:20:00,2110000003,7.83,462.19,3618.5,7331.5

这是我运行的代码:

library(data.table)
a<-fread("complete1.csv")
a[,`:=`(Timestamp=ymd_hms(Timestamp),
Date=ymd(Date),
SN=as.factor(SN))]
a[SN==c("2110000001","2110000002"),c("Timestamp","Date","Time","SN","A.Ms.Watt","Pac"),with=FALSE]

我得到这个输出:

   > a[SN==c("2110000001","2110000002"),c("Timestamp","Date","Time","SN","A.Ms.Watt","Pac"),with=FALSE]
             Timestamp       Date     Time         SN A.Ms.Watt     Pac
1: 2013-10-01 12:00:00 2013-10-01 12:00:00 2110000001   10723.0 13544.5
2: 2013-10-01 12:00:00 2013-10-01 12:00:00 2110000002    3704.0  6860.0
3: 2013-10-01 12:10:00 2013-10-01 12:10:00 2110000001   11278.5 14127.5
4: 2013-10-01 12:10:00 2013-10-01 12:10:00 2110000002    3952.0  7475.5
5: 2013-10-01 12:20:00 2013-10-01 12:20:00 2110000001   10279.5 12735.5
6: 2013-10-01 12:20:00 2013-10-01 12:20:00 2110000002    3929.0  7553.5
Warning messages:
1: In is.na(e1) | is.na(e2) :
  longer object length is not a multiple of shorter object length
2: In `==.default`(SN, c("2110000001", "2110000002")) :
  longer object length is not a multiple of shorter object length

不幸的是,我不太明白这些警告。但我每隔 12:xx:x5 间隔丢失一次数据(例如 12:00:05)。我可能做错了什么?

4

1 回答 1

6

这不是data.table问题,而是操作人员不当的问题。运算符==是矢量化的。看看当您查看时会发生什么:

a[,list(Timestamp,SN, SN == c("2110000001","2110000002"))]

              Timestamp         SN    V3
 1: 2013-10-01 12:00:00 2110000001  TRUE
 2: 2013-10-01 12:00:00 2110000002  TRUE
 3: 2013-10-01 12:00:00 2110000003 FALSE
 4: 2013-10-01 12:05:00 2110000001 FALSE
 5: 2013-10-01 12:05:00 2110000002 FALSE
 6: 2013-10-01 12:05:00 2110000003 FALSE
 7: 2013-10-01 12:10:00 2110000001  TRUE
 8: 2013-10-01 12:10:00 2110000002  TRUE
 9: 2013-10-01 12:10:00 2110000003 FALSE
10: 2013-10-01 12:15:00 2110000001 FALSE
11: 2013-10-01 12:15:00 2110000002 FALSE
12: 2013-10-01 12:15:00 2110000003 FALSE
13: 2013-10-01 12:20:00 2110000001  TRUE
14: 2013-10-01 12:20:00 2110000002  TRUE
15: 2013-10-01 12:20:00 2110000003 FALSE
Warning message:
In SN == c("2110000001", "2110000002") :
  longer object length is not a multiple of shorter object length

这记录在 R 语言手册的Operators中:

R 一次处理整个数据向量,并且大多数基本运算符和基本数学函数log都被向量化(如上表所示)。这意味着例如添加两个相同长度的向量将创建一个包含元素和的向量,隐式循环向量索引。这也适用于其他运算符,如-*/以及更高维结构。

如果您想要TRUEwhenSN是其中一个值c("2110000001","2110000002"),请使用%in%,例如

SN %in% c("2110000001","2110000002")
于 2013-11-15T04:12:17.130 回答