我有两个data.table
,我正在尝试将它们合并在一起。
第一个列有列:code
offers
第二个列有列:code
offers
nb_offers
我试图通过合并将第三列添加到第一个data.table。
这里提供了最小的文件:
我的问题是,对于某些行,这些值不会显示在此处。
一个工作示例:
> library(data.table)
> dt1 <- data.table(readRDS("dt1.rds"))
> dt2 <- data.table(readRDS("dt2.rds"))
> dt1[offers == "0.05"]
code offers
1: 24428 0.05
> dt2[offers == "0.05"]
code offers nb_offers
1: 24428 0.05 5
> merge(dt1[offers == "0.05"], dt2[offers == "0.05"],
by = c("code", "offers"))
code offers nb_offers
1: 24428 0.05 5
让我们稍微改变一下:
> dt1[offers == "0.06"]
code offers
1: 24428 0.06
> dt2[offers == "0.06"]
code offers nb_offers
1: 24428 0.06 3
> merge(dt1[offers == "0.06"], dt2[offers == "0.06"],
by = c("code", "offers"))
Empty data.table (0 rows) of 3 cols: code,offers,nb_offers
这对我来说毫无意义。
我试图复制错误,但是如果我从头开始创建data.tables,那么错误就不会发生:
> tmp1 <- data.table(code = "24428", offers = 0.06)
> tmp2 <- data.table(code = "24428", offers = 0.06, nb_offers = 3)
> setkey(tmp2, code)
> merge(tmp1, tmp2, by = c("code", "offers"))
code offers nb_offers
1: 24428 0.06 3
> all.equal(tmp1, dt1[offers == "0.06"])
[1] TRUE
> all.equal(tmp2, dt2[offers == "0.06"])
[1] TRUE
我知道我可以通过其他方式做我想做的事情,但我想了解这里发生了什么。
谢谢。