访问 X[Y, j] 的 j 中具有重复名称的 Y 列合并 [英] accessing Y columns with duplicated names in j of X[Y, j] merges

查看:15
本文介绍了访问 X[Y, j] 的 j 中具有重复名称的 Y 列合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的数据:

set.seed(1)
DT <- data.table(id=rep(1:3,each=3),y=1997L+sample(1:9,9))
DT2<- data.table(id=1:3,y=1997L+sample(1:3,3))

我想在与 DT 合并后使用 DT2$y.我看到这个列在合并后被命名为 y.1

I want to use DT2$y after merging with DT. I see that this column is named y.1 after the merge

setkey(DT,id)
names(DT[DT2])
# [1] "id"  "y"   "y.1"
DT[DT2][,y.1]
# [1] 1998 1998 1998 2000 2000 2000 1999 1999 1999

但是,我不能在 j 中使用该名称:

However, I cannot use it with that name in j:

DT[DT2,y.1]
# Error in `[.data.table`(DT, DT2, y.1) : object 'y.1' not found

我应该在这里使用什么秘密前缀或后缀?

What is the secret prefix or postfix that I should be using here?

推荐答案

可以用

DT[DT2, i.y]

如果您对它与 DT[DT2][, y.1] 的输出不同而感到惊讶,请参阅 这个帖子

and if you find yourself surprised that it's not the same output as DT[DT2][, y.1], see this thread

这篇关于访问 X[Y, j] 的 j 中具有重复名称的 Y 列合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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