R:将列表项变成对象

问题描述:

我有一个手动创建的对象列表,如下所示:

I have a list of objects that I've created manually, like this:

rand1 <- rnorm(1e3)
rand2 <- rnorm(1e6)

myObjects <- NULL
myObjects[[1]] <-rand1
myObjects[[2]] <-rand2
names(myObjects) <- c("rand1","rand2")

我正在研究一些将对象捆绑在一起并放在S3中的代码.然后我在EC2中有想要捕获myObjects列表并自动取消捆绑"的代码.在此示例中,列表只有两个对象,并且名称是已知的,但是我该如何编码以处理任何长度和任何名称的列表?

I'm working on some code that bundles up objects and puts them up in S3. Then I have code in EC2 that I want to grab the myObjects list and 'unbundle' it automatically. In this example the list only has two objects and the names are known, but how do I code this to handle lists of any length and any names?

#pseudo code
for each thing in myObjects
  thing <- myObjects[[thing]]

我不太清楚如何使用名称(myObjects)[1]并将其转换为对象的名称,我将为其分配myObjects [[1]]的内容.我可以处理循环,但是创建每个对象有点让我感到困惑.我敢肯定这很简单,但是我不能完全理解它.

I can't quite figure out how to take names(myObjects)[1] and turn it into the name of an object for which I will assign the contents of myObjects[[1]]. I can handle the looping but creating each object kinda has me hung. I'm sure this is quite simple, but I can't quite grok it.

您可以使用assign:

for(i in 1:length(myObjects)) assign(names(myObjects)[i], myObjects[[i]])