在 v1.8.3 之前,在 R {data.table} 中使用 `:=` 时如何抑制输出? [英] how to suppress output when using `:=` in R {data.table}, prior to v1.8.3?
问题描述
有没有办法防止 data.table
在通过引用分配新列后打印新的 data.table?我收集的标准行为是
Is there a way to prevent data.table
to print the new data.table after assigning a new column by reference? I gather standard behaviour is
library(data.table)
example(data.table)
DT
# x y v
# 1: a 1 42
# 2: a 3 42
# 3: a 6 42
# 4: b 1 11
# 5: b 3 11
# 6: b 6 11
# 7: c 1 7
# 8: c 3 8
# 9: c 6 9
DT[,z:=1:nrow(DT)]
# x y v z
# 1: a 1 42 1
# 2: a 3 42 2
# 3: a 6 42 3
# 4: b 1 11 4
# 5: b 3 11 5
# 6: b 6 11 6
# 7: c 1 7 7
# 8: c 3 8 8
# 9: c 6 9 9
即分配后表格打印到屏幕上.有没有办法在分配新列 z 后阻止 data.table 显示新表?我知道我可以通过说来阻止这种行为
i.e. the table is printed to screen after assignment. is there a way to stop data.table from showing the new table after assigning the new column z? I know I can stop this behaviour by saying
DT <- copy(DT[,z:=1:nrow(DT)])
但这违背了 :=
的目的(旨在避免复制).
but that is defeating the purpose of :=
(which is designed to avoid copies).
推荐答案
由于 <-.data.table
不会复制,所以可以使用 <-代码>:
Since <-.data.table
doesn't make a copy, you can use <-
:
创建一个data.table对象:
Create a data.table object:
library(data.table)
di <- data.table(iris)
创建一个新列:
di <- di[, z:=1:nrow(di)]
di
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species z
# [1,] 5.1 3.5 1.4 0.2 setosa 1
# [2,] 4.9 3.0 1.4 0.2 setosa 2
# [3,] 4.7 3.2 1.3 0.2 setosa 3
# [4,] 4.6 3.1 1.5 0.2 setosa 4
# [5,] 5.0 3.6 1.4 0.2 setosa 5
# [6,] 5.4 3.9 1.7 0.4 setosa 6
# [7,] 4.6 3.4 1.4 0.3 setosa 7
# [8,] 5.0 3.4 1.5 0.2 setosa 8
# [9,] 4.4 2.9 1.4 0.2 setosa 9
# [10,] 4.9 3.1 1.5 0.1 setosa 10
# First 10 rows of 150 printed.
<小时>
还值得记住的是,R 仅在交互模式下打印对象的值.
It is also worth remembering that R only prints the value of an object in interactive mode.
因此,在批处理模式下,您可以简单地使用:
So, in batch mode, you can simply use:
di[, z:=1:nrow(di)]
这在批处理模式下作为脚本运行时不会产生任何输出.
This will not produce any output when run as a script in batch mode.
来自 Matthew Dowle 的更多信息:
另请参阅常见问题解答 2.21 和 2.22:
Also see FAQ 2.21 and 2.22 :
2.21 为什么DT[i,col:=value]
返回整个DT
?我希望没有可见值(与 <-
一致),或者包含更新的行数的消息或返回值.数据确实被引用更新并不明显.
2.21 Why does
DT[i,col:=value]
return the whole ofDT
? I expected either no visible value (consistent with<-
), or a message or return value containing how many rows were updated. It isn't obvious that the data has indeed been updated by reference.
这样复合语法就可以工作了;例如,DT[i,done:=TRUE][,sum(done)]
.当详细程度打开时,将返回更新的行数,基于每个查询或全局使用 options(datatable.verbose=TRUE)
.
So that compound syntax can work; e.g., DT[i,done:=TRUE][,sum(done)]
. The number of rows updated is returned when verbosity is on, either on a per query basis or globally using options(datatable.verbose=TRUE)
.
2.22 好的,但是DT[i,col:=value]
的返回值不能不可见的返回吗?
2.22 Ok, but can't the return value of DT[i,col:=value]
be returned invisibly, then?
- 我们尝试过,但 R 在内部强制启用
[
的可见性.的价值FunTab 的[
的 eval 列(参见 src/main/names.c)是0
表示力R_Visible
开启(参见 R-Internals 第 1.6 节).因此,当我们尝试invisible()
或者自己直接设置R_Visible
为0
,eval
中src/main/eval.c 会再次强制它打开. - 在习惯了这种行为之后,您可能会越来越喜欢它(我们有).毕竟,我们使用
<-
进行了多少次子赋值,然后立即查看数据检查是否正常? - 我们可以将
:=
混入一个同样返回数据的j
中;混合更新并在一个查询中选择.检测j
是否单独更新(然后行为不同)可能会令人困惑.
- We tried to but R internally forces visibility on for
[
. The value of FunTab's eval column (see src/main/names.c) for[
is0
meaning forceR_Visible
on (see R-Internals section 1.6). Therefore, when we triedinvisible()
or settingR_Visible
to0
directly ourselves,eval
in src/main/eval.c would force it on again. - After getting used to this behaviour, you might grow to prefer it (we have). After all, how many times do we subassign using
<-
and then immediately look at the data to check it's ok? - We can mix
:=
into aj
which also returns data; a mixed update and select in one query. To detect whetherj
solely updates (and then behave dierently) could be confusing.
<小时>
Matthew Dowle 的第二次更新:
我们现在找到了解决方案,当使用 :=
时,v1.8.3 不再打印结果.我们将更新 FAQ 2.21 和 2.22.
We have now found a solution and v1.8.3 no longer prints the result when :=
is used. We will update FAQ 2.21 and 2.22.
这篇关于在 v1.8.3 之前,在 R {data.table} 中使用 `:=` 时如何抑制输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!