在数据表连接中,我可以访问`j`中重复的列名吗? [英] Can I access repeated column names in `j` in a data.table join?

查看:107
本文介绍了在数据表连接中,我可以访问`j`中重复的列名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试连接具有相同列名称的两个data.tables,则 .1 会附加到其中一个名称,但我似乎不能够访问 DT [] 表达式的 j 部分中的名称。

If I try to join two data.tables that have the same column names, then .1 is appended to one of the names, but I don't seem to be able to access the name in the j part of the DT[] expression.

示例:

DT1 = data.table(name = letters, value = rnorm(26))
DT2 = data.table(name = letters, value = rnorm(26))
setkey(DT1, name)
DT1[DT2, value.1 - value]       # this doesn't work
DT1[DT2][, value.1 - value]     # this works

这个问题的动机是,我认为单个调用会更快,原来不是这样,导致一个单独的问题为什么:为什么DT1 [DT2] [,value1-value]比具有较少列的data.table上的DT1 [DT2,value1-value]快?

The motivation for this question was that I thought the single call would be quicker, this turns out not to be the case, leading to a separate question of why: Why is DT1[DT2][, value1-value] faster than DT1[DT2, value1-value] on data.table with fewer columns?

推荐答案

您可以参考 data.table code>,即 DT2 的列,前缀 i 如下:

You can refer to columns of data.table in i, that is, DT2's columns, with the prefix i as follows:

DT1[DT2, list(val=i.value-value)]
   name val
1:    a   1
2:    b   1
3:    c   1
4:    d   1
5:    e   1







# Data used
DT1 <- data.table(name=letters[1:5], value=2:6)
DT2 <- data.table(name=letters[1:5], value=3:7)
setkey(DT1, name)

这篇关于在数据表连接中,我可以访问`j`中重复的列名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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