[R] how to automatically create objects with names from a string list?

Simon Blomberg s.blomberg1 at uq.edu.au
Wed Jun 4 08:10:02 CEST 2008


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.



More information about the R-help mailing list