< 是什么意思?代表在 data.table 中加入 on= [英] What does < stand for in data.table joins with on=

查看:18
本文介绍了< 是什么意思?代表在 data.table 中加入 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)] 返回 AY 中的 code>-column(i-data.table).

为了得到想要的结果,你可以这样做:

X[Y, on = .(A 

给出:

<块引用>

 A B1:1 12:2 13:3 1

在下一个版本中, 将返回两个 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)] 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, will return both A columns. See here for the discussion.

这篇关于&lt; 是什么意思?代表在 data.table 中加入 on=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆