[R] change object name within for loop

Erik Iverson eriki at ccbr.umn.edu
Fri Aug 20 21:39:52 CEST 2010



Gonçalo Ferraz wrote:
> Hi,
> 
> I am writing a for loop that creates one object, say 'outn' on every 
> round of the loop. I would like the name of each object to include the 
> index of the loop as in, for example:
> 
> out1, out2, out3, ...
> 
> And I would like the naming of the object to take place automatically as 
> the loop moves through?

Combine ?assign and ?paste, or better yet, avoid the use of this idiom
and store everything in a named list.  That way, you can use functions
like lapply to quickly work with your data, instead of resorting to
for loops for everything.

> 
> Similarly, I would like to be able to call different objects (in1, in2, 
> in3, ...) authomatically through the execution of the loop.

I don't know what that means. Possibly look at ?get.

> 
> Is there a quick solution for this? Thank you for any help.
> 
> I apologize if this question has been addressed recently in this forum - 
> I could not find an answer online.
> 
> Best,
> 
> Gonçalo
> 
> ______________________________________________
> 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