【发布时间】:2015-11-07 12:51:34
【问题描述】:
我试图在data.table 中理解rolling joins。最后给出了重现这一点的数据。
给定一个机场的交易数据表,在给定的时间:
> dt
t_id airport thisTime
1: 1 a 5.1
2: 3 a 5.1
3: 2 a 6.2
(注意t_ids 1 & 3 有相同的机场和时间)
以及从机场出发的航班查询表:
> dt_lookup
f_id airport thisTime
1: 1 a 6
2: 2 a 6
3: 1 b 7
4: 1 c 8
5: 2 d 7
6: 1 d 9
7: 2 e 8
> tables()
NAME NROW NCOL MB COLS KEY
[1,] dt 3 3 1 t_id,airport,thisTime airport,thisTime
[2,] dt_lookup 7 3 1 f_id,airport,thisTime airport,thisTime
我想将所有交易与从该机场起飞的所有下一个可能航班匹配,以提供:
t_id airport thisTime f_id
1 a 6 1
1 a 6 2
3 a 6 1
3 a 6 2
所以我认为这会起作用:
> dt[dt_lookup, nomatch=0,roll=Inf]
t_id airport thisTime f_id
1: 3 a 6 1
2: 3 a 6 2
但它没有返回交易t_id == 1。
来自the documentation 它说:
通常,x 的键中不应有重复项,...
但是,我的“x 键”(即airport 和thisTime)中确实有重复项,并且不能完全看到/理解发生了什么意味着t_id = 1 被从输出中删除。
谁能解释为什么t_id = 1 没有返回,当我有重复时如何让加入工作?
数据
library(data.table)
dt <- data.table(t_id = seq(1:3),
airport = c("a","a","a"),
thisTime = c(5.1,6.2, 5.1), key=c( "airport","thisTime"))
dt_lookup <- data.table(f_id = c(rep(1,4),rep(2,3)),
airport = c("a","b","c","d",
"a","d","e"),
thisTime = c(6,7,8,9,
6,7,8), key=c("airport","thisTime"))
【问题讨论】:
标签: r join data.table