按逻辑列子集 data.table [英] Subset data.table by logical column
本文介绍了按逻辑列子集 data.table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有逻辑列的 data.table
.为什么逻辑列的名称不能直接用于 i
参数?请参阅示例.
I have a data.table
with a logical column. Why the name of the logical column can not be used directly for the i
argument? See the example.
dt <- data.table(x = c(T, T, F, T), y = 1:4)
# Works
dt[dt$x]
dt[!dt$x]
# Works
dt[x == T]
dt[x == F]
# Does not work
dt[x]
dt[!x]
推荐答案
From ?data.table
高级:当 i
是单个变量名时,它不被视为列名的表达式,而是在调用范围内进行评估.
Advanced: When
i
is a single variable name, it is not considered an expression of column names and is instead evaluated in calling scope.
所以 dt[x]
将尝试在调用范围(在本例中为全局环境)评估 x
So dt[x]
will try to evaluate x
in the calling scope (in this case the global environment)
您可以通过使用 (
或 {
或 force
You can get around this by using (
or {
or force
dt[(x)]
dt[{x}]
dt[force(x)]
这篇关于按逻辑列子集 data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文