[R] create an object in a loop (v2)

David Winsemius dwinsemius at comcast.net
Mon Feb 1 18:47:49 CET 2010


On Feb 1, 2010, at 12:33 PM, Ivan Calandra wrote:

> I have a follow-up question:
>
> I use assign() to store some value in my paste()-created object as  
> suggested:
> for (i in 1:3) {
> assign(paste("object", i, sep=""), c("a", "b", "c"))
> }
>
> Then I would like to change the names of the elements of that object  
> within the loop. Since it is all in a loop, I cannot give the name  
> of the object manually by doing something like: names(object1) <-  
> c("tooth", "bone", "species").
> The only thing I can give to names() is paste("object", i, sep=""),  
> which doesn't work.
>
> Any idea of how to do it?

 > for (i in paste("object", 1:3, sep="")) {
+ assign(i, c("tooth"="a", "bone"="b", "species"="c") )
+ }
 > object1
   tooth    bone species
     "a"     "b"     "c"

>
> Thanks in advance
> Ivan
>
>
> Le 2/1/2010 17:14, David Winsemius a écrit :
>> Upon reading it yesterday, it appeared as it would have required  
>> some serious testing and there was no data on which to do any  
>> work.  You were clearly not taking the time to isolate the problem  
>> and construct a dataset. But who knows? When you say "What I want  
>> to do is. ... ,I would like the name of the list to be created in  
>> the loop too", maybe all you needed was to be pointed to was:
>>
>> ?assign
>>
>> But if that were the case, then you lost most of your audience  
>> along the way with a bunch of unneeded and obscure code.
>>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list