Mark Farnell
2008-Jun-04 05:15 UTC
[R] how to automatically create objects with names from a string list?
Suppose I have a string of objects names: name <- c("foo", "bar", "baz") and I would like to use a for loop to automatically create three objects called "foo", "bar" and "baz" accordingly. Then how can this be done" (so that in the workspace, foo = 1, bar = 2 and baz=3) for (i in name) { ..... } Thanks! Mark
Moshe Olshansky
2008-Jun-04 05:24 UTC
[R] how to automatically create objects with names from a string list?
You can use either assign or eval. Something like> name <- c("foo", "bar", "baz") > for (i in 1:length(name)) assign(name[i],i)--- On Wed, 4/6/08, Mark Farnell <mark.farnell at gmail.com> wrote:> From: Mark Farnell <mark.farnell at gmail.com> > Subject: [R] how to automatically create objects with names from a string list? > To: R-help at r-project.org > Received: Wednesday, 4 June, 2008, 3:15 PM > Suppose I have a string of objects names: > > name <- c("foo", "bar", > "baz") > > and I would like to use a for loop to automatically create > three > objects called "foo", "bar" and > "baz" accordingly. Then how can this > be done" (so that in the workspace, foo = 1, bar = 2 > and baz=3) > > for (i in name) { > ..... > } > > Thanks! > > Mark > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code.
Simon Blomberg
2008-Jun-04 06:10 UTC
[R] how to automatically create objects with names from a string list?
Or with mapply name <- c("foo", "bar", "baz") val <- 1:3 mapply(assign, name, val, pos=1) Cheers, Simon. On Tue, 2008-06-03 at 22:24 -0700, Moshe Olshansky wrote:> You can use either assign or eval. > > Something like > > > name <- c("foo", "bar", "baz") > > for (i in 1:length(name)) assign(name[i],i) > > > > --- On Wed, 4/6/08, Mark Farnell <mark.farnell at gmail.com> wrote: > > > From: Mark Farnell <mark.farnell at gmail.com> > > Subject: [R] how to automatically create objects with names from a string list? > > To: R-help at r-project.org > > Received: Wednesday, 4 June, 2008, 3:15 PM > > Suppose I have a string of objects names: > > > > name <- c("foo", "bar", > > "baz") > > > > and I would like to use a for loop to automatically create > > three > > objects called "foo", "bar" and > > "baz" accordingly. Then how can this > > be done" (so that in the workspace, foo = 1, bar = 2 > > and baz=3) > > > > for (i in name) { > > ..... > > } > > > > Thanks! > > > > Mark > > > > ______________________________________________ > > R-help at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, > > reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Simon Blomberg, BSc (Hons), PhD, MAppStat. Lecturer and Consultant Statistician Faculty of Biological and Chemical Sciences The University of Queensland St. Lucia Queensland 4072 Australia Room 320 Goddard Building (8) T: +61 7 3365 2506 http://www.uq.edu.au/~uqsblomb email: S.Blomberg1_at_uq.edu.au Policies: 1. I will NOT analyse your data for you. 2. Your deadline is your problem. The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. - John Tukey.
Greg Snow
2008-Jun-04 16:59 UTC
[R] how to automatically create objects with names from a string list?
Mark, Others have given answers to the question that you asked, in the spirit of fortune(108) I am going to answer some of the questions that you should have asked: Q1: Should I ever do this? Short answer: No Less short answer: probably not Longer answer: You should only do this once you fully understand why you should never do this. Q2: Why should I not do this? A: There are many reasons, the most important is probably that there are better ways to accomplish what you want to do. Another important reason is doing this tends to lead to bugs that are very difficult to find and debug, these types of constucts lead to accidentally overwriting variables, using a different variable than you intend, using loops where other tools are better, etc. Q3: What is a better method to accomplish this? A: This depends on what all you want to do with these new variables, but for most cases using a list (or sometimes creating a new environment) will accomplish what you want much better. For example:> tmp <- c('foo','bar','baz') > mylist <- as.list(1:3) > names(mylist) <- tmp > mylist$foo [1] 1 $bar [1] 2 $baz [1] 3> > # or > > mylist <- list() > for(i in seq(along=tmp)){+ mylist[[ tmp[i] ]] <- i + }> mylist$foo [1] 1 $bar [1] 2 $baz [1] 3> > # access a specific value > mylist$bar[1] 2> > # do something to all of them > sapply( mylist, function(x) x+5 )foo bar baz 6 7 8> > # use more directly > with( mylist, plot(1:10, 1:10, col=bar, pch=baz) ) > > # compute new variables based on them > mylist <- within(mylist, foobar <- foo + bar + baz) > mylist$foo [1] 1 $bar [1] 2 $baz [1] 3 $foobar [1] 6> > # save everything > save(mylist, file='myfile') > > # remove everything in one shot > rm(mylist) >Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Farnell > Sent: Tuesday, June 03, 2008 11:16 PM > To: R-help at r-project.org > Subject: [R] how to automatically create objects with names > from a string list? > > Suppose I have a string of objects names: > > name <- c("foo", "bar", "baz") > > and I would like to use a for loop to automatically create > three objects called "foo", "bar" and "baz" accordingly. > Then how can this be done" (so that in the workspace, foo > 1, bar = 2 and baz=3) > > for (i in name) { > ..... > } > > Thanks! > > Mark > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >