在数据表上的键控查找没有'with' [英] Keyed lookup on data.table without 'with'

查看:109
本文介绍了在数据表上的键控查找没有'with'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 data.table 这样的结构(除非我真的很大):

I have a data.table structure like so (except mine is really huge):

dt <- data.table(x=1:5, y=3:7, key='x')

我想通过名称为 x 的另一个变量在该结构中查找行 - 与 dt 的键名称相同:

I want to look up rows in that structure by another variable whose name is x (notice - the same as the name of the key of dt):

x <- 3:4
dt2 <- dt[ J(x) ]

这不工作,因为查找首先看到列名称,并且局部变量被遮蔽:

This doesn't work, because the lookup sees the column name first, and the local variable is obscured:

dt2
#    x y
# 1: 1 3
# 2: 2 4
# 3: 3 5
# 4: 4 6
# 5: 5 7

我想到了 with c $ c> [。data.table ,但仅适用于 j 参数,而不适用于 i 参数。

I thought about the with argument for [.data.table, but that only applies to the j argument, not the i argument.

i 参数中有类似的东西吗?

Is there something similar for the i argument?

如果没有,这样的事情会很方便,每当我使用一个局部变量,我不知道列名称在 dt 中的完整列表,以避免冲突。

If not, such a thing would be handy whenever I'm using a local variable and I don't know the complete list of column names in dt, to avoid conflicts.

推荐答案

在NEWS for 1.8.2中有一个项目建议

There is an item in the NEWS for 1.8.2 that suggests a ..() syntax will be added at some point, allowing this


新的DT [。(...)]语法将添加语法(在包plyr的样式中)与
DT [list(...)],DT [J(...)]和DT [data.table(...)]相同。我们计划添加..(),所以
。()和..()类似于文件系统的./和../;即,。()
在父范围内的DT和..()框架内进行计算。

New DT[.(...)] syntax (in the style of package plyr) is identical to DT[list(...)], DT[J(...)] and DT[data.table(...)]. We plan to add ..(), too, so that .() and ..() are analogous to the file system's ./ and ../; i.e., .() evaluates within the frame of DT and ..() in the parent scope.

平均时间,你可以从适当的环境 c $

In the mean time, you can get from the appropriate environment

dt[J(get('x', envir = parent.frame(3)))]
##    x y
## 1: 3 5
## 2: 4 6

或者您可以 eval 整个调用 list(x) J(x)

or you could eval the whole call to list(x) or J(x)

dt[eval(list(x))]
dt[eval(J(x))]
dt[eval(.(x))]

这篇关于在数据表上的键控查找没有'with'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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