gpt4 book ai didi

r - 在 data.table 中自索引时出错

转载 作者:行者123 更新时间:2023-12-02 02:02:33 27 4
gpt4 key购买 nike

这是我的意思的一个例子,它使事情变得清晰:

require(data.table)
x = data.table(a=1:10, idx=sample(c(TRUE, FALSE), 10, replace=TRUE))
x[idx]
Error in eval(expr, envir, enclos) : object 'idx' not found

但是,以下是有效的:

x[idx[]]
#a idx
#1: 2 TRUE
#2: 5 TRUE
#3: 7 TRUE
#4: 9 TRUE
#5: 10 TRUE

知道这里发生了什么吗?

最佳答案

引用@GSee 评论中提供的链接。

Hi, Yes expected. From ?data.table: "Advanced: When i is a single variable name, it is not considered an expression of column names and is instead evaluated in calling scope." Subsetting by a logical column is the only example I can think of where this is confusing. But we make use of this feature quite a lot e.g. TMP=list(...);DT[TMP] safe in the knowledge that DT[TMP] won't start to fail if DT in future has a column called TMP. When I have a logical column boolCol I wrap with (): DT[(boolCol)].
This avoids the memory allocation and scan of ==TRUE, and avoids the variable name repetition of DT[DT$boolCol] Matthew

关于r - 在 data.table 中自索引时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512086/

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