什么是<在data.table中代表on = [英] What does < stand for in data.table joins with on=
问题描述
联接数据表:
X <- data.table(A = 1:4, B = c(1,1,1,1))
# A B
# 1: 1 1
# 2: 2 1
# 3: 3 1
# 4: 4 1
Y <- data.table(A = 4)
# A
# 1: 4
通过
X[Y, on = .(A == A)]
# A B
# 1: 4 1
返回预期结果.但是,我希望这行代码:
returns the expected result. However, I would expect the line:
X[Y, on = .(A < A)]
# A B
# 1: 4 1
# 2: 4 1
# 3: 4 1
返回
A B
1: 1 1
2: 2 1
3: 3 1
因为关键字on
:
指示应将x中的哪些列与i中的哪些列进行连接,以及要与之连接的二进制运算符的类型
Indicate which columns in x should be joined with which columns in i along with the type of binary operator to join with
根据?data.table
.没有明确提到加入的方式,当然也没有像我所猜测的那样. <
如何精确地将x中的列与i中的列连接起来?
according to ?data.table
. The way the joining is done is not explicitly mentioned, and certainly it is not as I have guessed. How exactly <
joins columns in x with columns in i?
推荐答案
进行非等额联接时,如X[Y, on = .(A < A)]
data.table 返回Y
的A
列(i
-data.table).
When doing a non-equi join like X[Y, on = .(A < A)]
data.table returns the A
-column from Y
(the i
-data.table).
要获得理想的结果,您可以执行以下操作:
To get the desired result, you could do:
X[Y, on = .(A < A), .(A = x.A, B)]
给出:
A B
1: 1 1
2: 2 1
3: 3 1
在下一个版本中,data.table 将同时返回两个A
列. 请参阅此处进行讨论.
In the next release, data.table will return both A
columns. See here for the discussion.
这篇关于什么是<在data.table中代表on =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!