按索引列和行的子集数据表 [英] subset data.table by indexed column and rows
本文介绍了按索引列和行的子集数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望通过更改列 z
的索引并同时基于某些筛选行来递归地对数据表进行子集化基于%in%
的向量。
I am looking to subset a data table recursively, by changing the index of the column z
AND at the same time filter rows based on some %in%
based vector.
dt <- setDT(copy(diamonds))
dt <- setDT(data.frame(lapply(dt, as.character), stringsAsFactors=FALSE))
z=4
subset_by <- unique(dt[,z])[1:2]
### obviously does not work
###dt1<-dt[ z %in% subset_by]
我正在寻找最节省内存的操作来执行此操作,并且我敢肯定有一种方法不使用colnames,但是我只是找不到它。我看了很多帖子,其中此最相关的
I am looking for the most memory-efficient operation to do this and I am sure there is a way without using colnames, but I just cannot find it. I looked at a lot of posts, with this beign the most relevant
推荐答案
如果我们根据索引或名称进行子集设置,则可以在中进行指定。 SDcols
If we are subsetting based on the index or names, we can specify it in .SDcols
i1 <- dt[, .I[.SD[[1]] %chin% subset_by], .SDcols = z]
dt[i1]
data.table / tbl_df / data_frame
中的列将是 [[或
$
subset_by <- unique(dt[[z]])[1:2]
这篇关于按索引列和行的子集数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文