r - `.Random.seed` 和 `ls()` 之间有什么关系?
问题描述
load("files.RData"); ls()
打印到控制台的输出是:
'File1' 'File2' 'File3'
当这样做时(load("files.RData"))
(括号的点load()
是指示 R 打印该行的输出)打印到控制台的输出是:
'.Random.seed' 'File1' 'File2' 'File3'
问题:造成这种差异的原因是什么?
注意:这可能特定于 IRkernel,因为此代码正在 Jupyter 笔记本中运行。
解决方案
From help("ls")
, we can see the purpose of the all.names
argument to ls()
, which has a default value of FALSE
:
all.names: a logical value. If ‘TRUE’, all object names are returned.
If ‘FALSE’, names which begin with a ‘.’ are omitted.
So, in your first example, ls()
will not print .Random.seed
; it begins with a .
.
Now let us consider the "Value" sections of the help files for load
:
A character vector of the names of objects created, invisibly.
and Paren
:
For ‘(’, the result of evaluating the argument. This has
visibility set, so will auto-print if used at top-level.
So, in your second example, load("files.RData")
invisibly returns "A character vector of the names of objects created" (even .Random.seed
), but (
auto-prints that character vector, even including .Random.seed
.
What even is .Random.seed
?
First, we can see what it is by looking at help(".Random.seed")
:
‘.Random.seed’ is an integer vector, containing the random number
generator (RNG) *state* for random number generation in R. It can
be saved and restored, but should not be altered by the user.
It will pop up in your global environment any time you use one of R's pseudo-random number generators. For example, in a fresh R session, I can do the following:
x <- 1
ls(all.names = TRUE)
# [1] "x"
rnorm(1)
# [1] 2.378572
ls(all.names = TRUE)
# [1] ".Random.seed" "x"
Then I can save any or all of these R objects via save()
:
save(x, file = "one.RData")
save(.Random.seed, file = "two.RData")
save(x, .Random.seed, file = "all.RData")
# or, equivalently in this case,
# save(list = ls(all.names = TRUE), file = "all.RData")
If I use save.image()
, everything in my global environment is saved, even files beginning with .
-- according to the help file, it's a shortcut for save(list = ls(all.names = TRUE), file = ".RData", envir = .GlobalEnv)
.
So, wherever you got the files.RData
from, they either used save.image()
, or intentionally included their .Random.seed
object when saving files.RData
.
推荐阅读
- r - 计算一列中有多少字符出现在另一列中
- scala - Kotlin(和 scala):方法 scala.collection.TraversableOnce.reverser$2 中没有索引为 0-0 的参数(名称=reverser$module$1 访问=16)
- ansible - ``apt-mark hold`` 和 ``apt-mark unhold`` 与 ansible 模块
- php - 致命错误:未捕获的错误:不能在 C:\xampp\htdocs\MyShoppingList\list.php:487 中使用 stdClass 类型的对象作为数组
- c# - 如何用字符替换二维数组中的空字符串?
- python - 数据框:单元格级别:将逗号分隔的字符串转换为列表
- unity3d - unity 对象跟随另一个对象方向
- python - 如何在密度热图上添加风矢量?
- python - 如何在机器学习中查看预测的特征重要性
- android - 这个 R.id.home 是什么?