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

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Wed, 04 Jun 2008 10:59:27 -0600

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.
>
______________________________________________ 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 - 18:52:10 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 - 20:31:20 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