子集数据表,不使用< - [英] Subset data table without using <-
问题描述
我想要子集数据表的一些行。像这样:
I want to subset some rows of a data table. Like this:
# load data
data("mtcars")
# convert to data table
setDT(mtcars,keep.rownames = T)
# Subset data
mtcars <- mtcars[like(rn,"Mer"),] ; or
mtcars <- mtcars[mpg > 20,]
但是,我正在使用一个巨大的数据集, code>< - ,这不是内存高效的,因为它使数据的副本。
However, I'm working with a huge data set and I wanted to avoid using <-
, which is not memory efficient because it makes a copy of the data.
这样是否正确?
是否可以更新过滤的数据,而不需要 < -
?
推荐答案
您要求的内容是按参考删除列。
这还不可能,但是#635 。
在此之前,您需要复制(内存中)您的data.table子集,复制由< - $ c当与子集合(
i
arg)组合时,$ c>(或 =
),所以现在你不能避免。
Until then you need to copy (in-memory) your data.table subset, the copy is done by <-
(or =
) when is combined with subset (i
arg) so for now you cannot avoid that.
如果它能帮助你以某种方式操作语言对象来预定义操作并延迟它的评估,还可以重复使用预定义对象多次:
If it will help somehow you can operate on language objects to predefine the operation and delay it's evaluation, also reuse predefined objects multiple times:
mtcars_sub <- quote(mtcars[like(rn,"Mer")])
mtcars_sub2 <- quote(eval(mtcars_sub)[mpg > 20])
eval(mtcars_sub2)
# rn mpg cyl disp hp drat wt qsec vs am gear carb
# 1: Merc 240D 24.4 4 146.7 62 3.69 3.19 20.0 1 0 4 2
# 2: Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
BTW。当子集化datas.table你不需要使用中间逗号像 dt [x == 1,]
你可以使用 dt [x == 1]
。
BTW. when subsetting data.table you don't need to use middle comma like dt[x==1,]
you can use dt[x==1]
.
这篇关于子集数据表,不使用< - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!