R: turning list items into objects


Question: 

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")

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]]

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.




3 Answers: 

You can use assign:

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

attach(myObjects)

 

To expand Shane's answer:

mapply(assign, names(myObjects), myObjects, MoreArgs=list(envir = globalenv())

(You may wish to change globalenv() to another environment.)

 

More Articles


Computer Networks ip addressing

So I have this exercise in one of my classes on network ip addressing. The problem is that i cant figure out how to complete the subnet bits and hosts bits..can anyone help me?the number of hosts is 414, 189, 135 and 90...

javascript - less.js: Use custom import function

less.js is using an internal xhr() function to load @imported .less files dynamically via ajax.I want to know if there is anything I can do the hand a custom function over to the less parser to get the imported files loaded through this function and NOT through the default loading function.As a wild

php - Laravel default selection on form when loading page

I have a select tag on a form, and I want to populate a default option, this default option will be the one that is searched. I can get it through something like $_GET but it does not work in my case, as I am looping through and getting all the values from the database. <select name="location"&


exchange server - What e-mail address ("From" vs. "Mail From") is considered by my block list?

We are getting a lost of spam /phishing e-mails where the [From] is showing our own domain (e.g. "john@foobar.com"), but the [Mail From] (return-path) in the e-mail header is showing "root@foo-574401.hostbar.com".Question is now, which e-mail address is actually considered by the Exchange Online blo

How to use binary matrix in CSV for apriori alogorithm in r

I am trying to use the binary matrix containing transactions for the apriori algorithm I don't know how to implement it data_purchase Txn Bag Blush Nail.Polish Brushes Concealer Eyebrowpencil Bronzer1 1 0 1 1 1 1 0 12 2 0 0 1

scalatest - Confusing type mismatch in Scala

I have:val words = List("all", "the", "words", "all", "the", "counts", "all", "day")val map = Exercise02.count(words.iterator)val expected = Map("all" -> 3, "the" -> 2, "words" -> 1, "counts" -> 1, "day" -> 1)where Exercise02.count is java.util.Iterator[String] => Map[String, Int]


Apply function to one element of a list in Python

I'm looking for a concise and functional style way to apply a function to one element of a tuple and return the new tuple, in Python.For example, for the following input:inp = ("hello", "my", "friend")I would like to be able to get the following output:out = ("hello", "MY", "friend")I came up with t

authentication - Sitecore - Prevent access to a page, but still show it in the navigation

In Sitecore I have denied access to a particular page for the anonymous user. This works correctly, but it also means that the page does not appear in the navigation menus and sitemap (both XSLT).What I would like is for the user to be able to see the link, but be redirected to a Register/Login page

scala - Should I use collectionAsScalaIterable({java collection}) or Seq({java collection}).flatten?

We're starting to use Scala Test to test our Java application, and I want to test the contents of a Java Collection. We came up with 2 possibilities:JavaConversions.collectionAsScalaIterable(getJavaCollection()) must contain(allOf(item1, item2).inOrder)orSeq(getJavaCollection()).flatten mustEqual Se

Revert from Python 2.7 to Python 2.6 in Mac OSX 10.6.8 doesn't work even though I've uninstalled Python 2.7

I have mac OSX 10.6.8. My system came with Python 2.6I recently installed Python 2.7 but experienced many problems with it so decided to revert back to Python 2.6 by executing $ sudo rm -rf /Library/Frameworks/Python.framework/Versions/2.7I also removed Python 2.7 from my Applications folder. Still