名称(.GlobalEnv)如何排序?

时间:2019-07-11 14:52:31

标签: r names

names的文档中说:

  

对于环境env,names(env)给出相应列表的名称,即names(as.list(env,all.names = TRUE)),也由ls(env,all.names = TRUE,排序= FALSE)。

我尝试过names(x = .GlobalEnv),但实际上它们不是按字母顺序排序的。我的问题是如何订购?

考虑以下示例:

rm(list = ls())
temp <- vector(mode = "list", length = 20)
for (i in 1:20)
{
  assign(x = paste("vec", i, sep = "_"),
         value = rnorm(n = 10))
  temp[[i]] <- names(x = .GlobalEnv)
}
temp_1 <- temp
save(temp_1, file = "try_1.RData")

rm(list = ls())
temp <- vector(mode = "list", length = 20)
for (i in 1:20)
{
  assign(x = paste("vec", i, sep = "_"),
         value = rnorm(n = 10))
  temp[[i]] <- names(x = .GlobalEnv)
}
temp_2 <- temp
save(temp_2, file = "try_2.RData")

rm(list = ls())
load(file = "try_1.RData")
load(file = "try_2.RData")
all.equal(target = temp_1, current = temp_2)
#> [1] "Component 18: 21 string mismatches"
#> [2] "Component 19: 22 string mismatches"
#> [3] "Component 20: 23 string mismatches"
temp_1[[20]]
#>  [1] "vec_14"       "vec_15"       "vec_16"       "vec_17"      
#>  [5] "vec_18"       "vec_1"        "vec_19"       "vec_2"       
#>  [9] "vec_3"        "vec_4"        "vec_5"        "vec_6"       
#> [13] "vec_7"        "vec_8"        "vec_20"       "vec_9"       
#> [17] ".Random.seed" "temp"         "i"            "vec_10"      
#> [21] "vec_11"       "vec_12"       "vec_13"
temp_2[[20]]
#>  [1] "i"            "vec_10"       "vec_11"       "vec_12"      
#>  [5] "vec_13"       "vec_14"       "vec_15"       "vec_16"      
#>  [9] "vec_1"        "vec_17"       "vec_2"        "vec_18"      
#> [13] "vec_19"       "vec_3"        "temp"         "vec_4"       
#> [17] "vec_5"        ".Random.seed" "vec_6"        "vec_7"       
#> [21] "vec_8"        "vec_9"        "vec_20"

reprex package(v0.3.0)于2019-07-11创建

从上面可以看出,定义对象的相同顺序在names(x = .GlobalEnv)中不会导致相同的顺序。他们是随机订购的吗?

这个问题的灵感来自this thread

0 个答案:

没有答案