R:在子集化期间丢失数据(data.table)

时间:2013-11-15 04:02:02

标签: r data.table

当我尝试对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)丢失数据。我能做错什么?

1 个答案:

答案 0 :(得分: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被矢量化(如上表所示)。这意味着,例如添加两个相同长度的向量将创建一个包含元素和的向量,隐式地循环遍历向量索引。这也适用于-*/等其他运营商以及更高维度的结构。

如果您希望TRUE SN为值c("2110000001","2110000002"),请使用%in%,例如

SN %in% c("2110000001","2110000002")