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? Similarly, I would like to be able to call different objects (in1, in2, in3, ...) authomatically through the execution of the loop. 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
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.
The quick answer is to use a list. The most simple: outlist=list() for (i in 1:10){ outlist[[i]] = matrix(rnorm(100), 10, 10) } Same example, but with naming: outlist=list() for (i in 1:10){ outlist[[i]] = data.frame(loop_number=i, matrix(rnorm(100), 10, 10)) } now if you were to do final_output=do.call('rbind', outlist) You'd have something that you can probably work with. str(final_output) Lists are kind of weird, but they're worth exploring. Remember that the output of [ is a list and [[ returns the actual item. So to get the third data.frame you'd want to use outlist[[3]] 2010/8/20 Gonçalo Ferraz <gferraz29@gmail.com>> 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? > > Similarly, I would like to be able to call different objects (in1, in2, > in3, ...) authomatically through the execution of the loop. > > 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@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. >[[alternative HTML version deleted]]
On Aug 20, 2010, at 1:07 PM, 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, ...?assign ?paste> > And I would like the naming of the object to take place > automatically as the loop moves through? > > Similarly, I would like to be able to call different objects (in1, > in2, in3, ...) authomatically through the execution of the loop.?get ?paste> > Is there a quick solution for this? Thank you for any help.There are many worked examples in the archives.> > I apologize if this question has been addressed recently in this > forum - I could not find an answer online.How did you search?> > Best, > > Gon?alo-- David.
Seemingly Similar Threads
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON