英文:
Subsetting by reference in `data.table`
问题
我想知道是否可以通过引用来对数据表进行子集筛选。
一个涉及通过赋值进行更新的解决方案是:
iris <- as.data.table(iris)
iris <- iris[Species == "virginica"]
这种方法的缺点是它复制了筛选后的数据表。如果可能的话,我想通过引用进行筛选,可能使用 :=
运算符和 .SD
快捷方式。
英文:
I am wondering if it is possible to subset a data.table by reference.
A solution that involves updating by assignment is:
iris <- as.data.table(iris)
iris <- iris[Species == "virginica"]
The downside of this approach is it copies the filtered data.table. If possible, I would like to filter by reference, possibly using the :=
operator and the .SD
shortcut.
答案1
得分: 1
参考https://stackoverflow.com/questions/10790204/how-to-delete-a-row-by-reference-in-data-table
你可以通过引用修改data.table
的子集,如下所示:
iris <- as.data.table(iris)
address(iris)
#> [1] "000001dc87099330"
iris
的行子集需要在内存中创建一个新对象。
iris2 <- iris[Species == "virginica"]
address(iris2)
#> [1] "000001dc876cdc30"
可以通过引用修改列的子集,或者添加/删除列。
iris[Species == "virginica", Species := "virg"]
address(iris)
#> [1] "000001dc87099330"
iris[,Species := NULL]
address(iris)
#> [1] "000001dc87099330"
iris[,Species := "virginica"]
address(iris)
#> [1] "000001dc87099330"
英文:
Refer to https://stackoverflow.com/questions/10790204/how-to-delete-a-row-by-reference-in-data-table
You can modify a subset of a data.table
by reference, though:
iris <- as.data.table(iris)
address(iris)
#> [1] "000001dc87099330"
An object that is a row subset of iris
requires a new object in memory.
iris2 <- iris[Species == "virginica"]
address(iris2)
#> [1] "000001dc876cdc30"
Modifying a subset of a column or adding/deleting a column can be done by reference.
iris[Species == "virginica", Species := "virg"]
address(iris)
#> [1] "000001dc87099330"
iris[,Species := NULL]
address(iris)
#> [1] "000001dc87099330"
iris[,Species := "virginica"]
address(iris)
#> [1] "000001dc87099330"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论