< 是什么意思?代表在 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 1Y <- data.table(A = 4)# 一个# 1:4
通过
X[Y, on = .(A == A)]# A B# 1:4 1
返回预期结果.但是,我希望这条线:
X[Y, on = .(A < A)]# A B# 1:4 1#2:4 1# 3:4 1
返回
A B1:1 12:2 13:3 1
因为关键字on
:
指出 x 中的哪些列应该与 i 中的哪些列以及要连接的二元运算符的类型进行连接
根据?data.table
.没有明确提到加入的方式,当然也不是我猜到的.<
究竟是如何将 x 中的列与 i 中的列连接起来的?
当进行像 X[Y, on = .(A < A)]
data.table 返回 A
Y
中的 code>-column(i
-data.table).
为了得到想要的结果,你可以这样做:
X[Y, on = .(A
给出:
<块引用> A B1:1 12:2 13:3 1
在下一个版本中,data.table 将返回两个 A
列.查看此处进行讨论.
Joining the data tables:
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
via
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
to return
A B
1: 1 1
2: 2 1
3: 3 1
because the keyword on
:
Indicate which columns in x should be joined with which columns in i along with the type of binary operator to join with
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?
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)]
which gives:
A B 1: 1 1 2: 2 1 3: 3 1
In the next release, data.table will return both A
columns. See here for the discussion.
这篇关于< 是什么意思?代表在 data.table 中加入 on=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!