gpt4 book ai didi

r - 在没有 'with' 的情况下对 data.table 进行键控查找

转载 作者:行者123 更新时间:2023-12-04 16:46:56 26 4
gpt4 key购买 nike

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

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

我想通过另一个名称为 x 的变量查找该结构中的行(注意 - 与 dt 的键名相同):
x <- 3:4
dt2 <- dt[ J(x) ]

这不起作用,因为查找首先看到列名,而局部变量被遮蔽:
dt2
# x y
# 1: 1 3
# 2: 2 4
# 3: 3 5
# 4: 4 6
# 5: 5 7

我想到了 with [.data.table 的论据,但这仅适用于 j参数,而不是 i争论。
i 是否有类似的东西?争论?

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

最佳答案

在 1.8.2 的新闻中有一条建议 ..()语法将在某个时候添加,允许这样做

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.



同时,您可以 get从合适的环境
dt[J(get('x', envir = parent.frame(3)))]
## x y
## 1: 3 5
## 2: 4 6

或者你可以 eval全程调用 list(x)J(x)
dt[eval(list(x))]
dt[eval(J(x))]
dt[eval(.(x))]

关于r - 在没有 'with' 的情况下对 data.table 进行键控查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com