在`data.table`中通过引用进行子集筛选

huangapple go评论65阅读模式
英文:

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 &lt;- as.data.table(iris)

iris &lt;- iris[Species == &quot;virginica&quot;]

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 &lt;- as.data.table(iris)
address(iris)
#&gt; [1] &quot;000001dc87099330&quot;

An object that is a row subset of iris requires a new object in memory.

iris2 &lt;- iris[Species == &quot;virginica&quot;]
address(iris2)
#&gt; [1] &quot;000001dc876cdc30&quot;

Modifying a subset of a column or adding/deleting a column can be done by reference.

iris[Species == &quot;virginica&quot;, Species := &quot;virg&quot;]
address(iris)
#&gt; [1] &quot;000001dc87099330&quot;
iris[,Species := NULL]
address(iris)
#&gt; [1] &quot;000001dc87099330&quot;
iris[,Species := &quot;virginica&quot;]
address(iris)
#&gt; [1] &quot;000001dc87099330&quot;

huangapple
  • 本文由 发表于 2023年6月15日 02:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定