我有两个表,如下所示:
library(data.table)
Input<-data.table("Date"=seq(1:10),"Cycle"=c(90,100,130,180,200,230,250,260,300,NA))
Date Cycle
1: 1 90
2: 2 100
3: 3 130
4: 4 180
5: 5 200
6: 6 230
7: 7 250
8: 8 260
9: 9 300
10: 10 320
FDate<-data.table("Date"=seq(1:9),"Cycle"=c(90,100,130,180,200,230,250,260,300),"Task"=c("D","A","B,C",NA,"A,D","D","C","D","A,C,D"))
Date Cycle Task
1: 1 90 D
2: 2 100 A
3: 3 130 B,C
4: 4 180 <NA>
5: 5 200 A,D
6: 6 230 D
7: 7 250 C
8: 8 260 D
9: 9 300 A,C,D
我只想有一个不重叠Date
且对应的输出表Cycle
。我尝试过,setdiff
但没有用。我希望我的输出是这样的
Date Cycle
10 320
当我尝试这个时,setdiff(FDate$Date,Input$Date)
它变成这样integer(0)
我们可以通过仅在两个数据集中包括公共列来使用fsetdiff
fromdata.table
fsetdiff(Input, FDate[ , names(Input), with = FALSE])
# Date Cycle
#1: 10 320
或@Frank提到的加入
Input[!FDate, on=.(Date)]
# Date Cycle
#1: 10 320
在OP的代码中,
setdiff(FDate$Date,Input$Date)
第一个参数来自“ FDate”的“ Date”列,该列中的所有元素也位于主数据“ Input $ Date”中。因此,它返回integer(0)
)。如果我们做相反的事情,它将返回10
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句