【问题标题】:Why does .SD in a data.table non-equi join throws an error occasionally?为什么 data.table 非 equi 连接中的 .SD 偶尔会抛出错误?
【发布时间】:2019-06-07 07:35:09
【问题描述】:

我注意到在非 equi 连接中使用 .SD 时出现不一致。 有对此的解释吗?

根据连接的“方向”或“类型”,使用j = .SD 会引发错误。

library(data.table)
d1 <- fread("a, b
            1, 11
            6, 16")
d2 <- data.table(r = 1:5, s = seq(0, 20, 5))

d1
   a  b
1: 1 11
2: 6 16
d2
   r  s
1: 1  0
2: 2  5
3: 3 10
4: 4 15
5: 5 20
d1[d2, on = .(a <= s, b >= s)]
    a  b r
1:  0  0 1
2:  5  5 2
3: 10 10 3
4: 10 10 3
5: 15 15 4
6: 20 20 5
d1[d2, on = .(a <= s, b >= s), j = .SD]

[.data.table(d1, d2, on = .(a = s), j = .SD) 中的错误:
未找到列:a

d2[d1, on = .(s >= a, s <= b)]
   r s s.1
1: 2 1  11
2: 3 1  11
3: 3 6  16
4: 4 6  16
d2[d1, on = .(s >= a, s <= b), j = .SD]
   r s
1: 2 1
2: 3 1
3: 3 6
4: 4 6

我已经用 R 版本 3.6.0 和 data.table 版本 1.11.8、1.12.2 和 1.12.3(github 上的开发版本)重现了该行为。

我知道在 github 上有相关的讨论,例如,Both columns for rolling and non-equi joins #3093.SD in expression with j? #3115,但我没有找到(也许被忽视了?)对那里观察到的行为的解释。

【问题讨论】:

标签: r data.table


【解决方案1】:

感谢 Arun 和 Matt,issue 已成为 fixed (note item 24),最新开发版本为 data.table 1.12.3。

所以,

d1[d2, on = .(a <= s, b >= s), j = .SD]

不再抛出错误而是返回

    a  b
1:  0  0
2:  5  5
3: 10 10
4: 10 10
5: 15 15
6: 20 20

正如预期的那样。

【讨论】:

    猜你喜欢
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    相关资源
    最近更新 更多