R:将环境名称设置为new.env()的参数

时间:2017-10-16 21:59:50

标签: r environment-variables

有没有办法在调用new.env()时将“indian”,“pizza”和“ramen”三个名称初始化为参数?

eats <- new.env(
    indian = "8elements",
    pizza = "blue line",
    ramen = "orenchi")

这可以替代:

eats <- new.env()
eats$indian = "8elements"
eats$pizza = "blue line"
eats$ramen = "orenchi"

1 个答案:

答案 0 :(得分:5)

以下是一些替代方案:

1)创建一个列表并将其转换为环境

eats <- list2env(list(
    indian = "8elements",
    pizza = "blue line",
    ramen = "orenchi"))

2)另一种可能性是使用local,如下所示:

eats <- local({ 
  indian = "8elements"
  pizza = "blue line"
  ramen = "orenchi"
  environment()
})

3) proto包创建的proto对象实际上是环境:

library(proto)
eats <- proto(
  indian = "8elements",
  pizza = "blue line",
  ramen = "orenchi")