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

Moshe Olshansky m_olshansky at yahoo.com
Wed Jun 4 07:24:30 CEST 2008


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.



More information about the R-help mailing list