从子表化数据表中检索特定值 [英] Retrieving specific values from subsetting a data.table
本文介绍了从子表化数据表中检索特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在R中有一个 data.table
:
my.dt <- data.table(x=seq(1:5),y=(c(TRUE, TRUE, FALSE, FALSE, FALSE)))
我想从中提取单个值或一个向量值:
I want to extract a single value, or a vector of values from this:
boolean.vector <- my.dt[x<4,"y",with=FALSE]
boolean.value <- my.dt[x<2,"y",with=FALSE]
$ b $ p
但是,这些返回值仍然属于 data.table
。因此,我不能,例如,做以下:
However, these return values are still of class data.table
. As such, I cannot, for example, do the following:
> if(boolean.value) { print("Hello") }
Erro em if (boolean.value) { : argumento não é interpretável como lógico
# (Error in if (boolean.value) { : argument cannot be interpreted as logical)
如何检索原始值,方式?
How can I retrieve the raw values so I can use them in this manner?
推荐答案
而不是
my.dt[1:3,"y",with=FALSE]
只是做
my.dt$y[1:3]
或
my.dt[["y"]][1:3]
或
my.dt[x<2,"y",with=FALSE][[1]]
在基本R [[
不复制向量(它是副本on-change),所以这是非常高效的,不需要为此使用 data.table
语法。
In base R [[
doesn't copy the vector (it's copy-on-change), so this is very efficient and there isn't any need to use data.table
syntax for this.
这篇关于从子表化数据表中检索特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文