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

From: Moshe Olshansky <m_olshansky_at_yahoo.com>
Date: Tue, 03 Jun 2008 22:24:30 -0700 (PDT)


You can use either assign or eval.

Something like

> name <- c("foo", "bar", "baz")
> for (i in 1:length(name)) assign(name[i],i)

> From: Mark Farnell <mark.farnell_at_gmail.com>
> Subject: [R] how to automatically create objects with names from a string list?
> To: R-help@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. Received on Wed 04 Jun 2008 - 05:28:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 04 Jun 2008 - 06:30:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive