Dear Admin, I will appreciate if you advise me an effective way to write the following R code including nested for loops. I cannot do it by using expand.grid function because it results with memory allocation problems. Thanks for your time and consideration. for(d1 in 0:n){ for(d2 in 0:n){ for(d3 in 0:n){ for(d4 in 0:n){ for(d5 in 0:n){ for(d6 in 0:n){ for(d7 in 0:n){ for(d8 in 0:n){ for(d9 in 0:n){ for(d10 in 0:n){ for(d11 in 0:n){ for(d12 in 0:n){ for(d13 in 0:n){ for(d14 in 0:n){ for(d15 in 0:n){ for(d16 in 0:n){ for(d17 in 0:n){ for(d18 in 0:n){ for(d19 in 0:n){ for(d20 in 0:n){ list=c(d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20) }}}}}}}}}}}}}}}}}}}} [[alternative HTML version deleted]]
Le 05/07/10 23:06, Senay ASMA a ?crit :> > Dear Admin, > I will appreciate if you advise me an effective way to write the following R > code including nested for loops. I cannot do it by using expand.grid > function because it results with memory allocation problems. > Thanks for your time and consideration. > > for(d1 in 0:n){ > for(d2 in 0:n){ > for(d3 in 0:n){ > for(d4 in 0:n){ > for(d5 in 0:n){ > for(d6 in 0:n){ > for(d7 in 0:n){ > for(d8 in 0:n){ > for(d9 in 0:n){ > for(d10 in 0:n){ > for(d11 in 0:n){ > for(d12 in 0:n){ > for(d13 in 0:n){ > for(d14 in 0:n){ > for(d15 in 0:n){ > for(d16 in 0:n){ > for(d17 in 0:n){ > for(d18 in 0:n){ > for(d19 in 0:n){ > for(d20 in 0:n){ > > list=c(d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20) > }}}}}}}}}}}}}}}}}}}}Probably not what you want, but this should replicate the same effect as the code you posted: list <- rep( n, 20 ) Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://bit.ly/98Uf7u : Rcpp 0.8.1 |- http://bit.ly/c6YnCi : graph gallery collage `- http://bit.ly/bZ7ltC : inline 0.3.5
What do you want to do with the data being genereated? In the loop you have, it will just return the last value generated. Let me ask my favorite question: "What is the problem you are trying to solve". If you get a memory problem with expand.grid, then if you are trying to store the values in the 'for' loop, you will have the same problem. How big is 'n'? If it is 3, you will have this many values:> 3^20[1] 3486784401 What are you going to do with them? On Mon, Jul 5, 2010 at 5:06 PM, Senay ASMA <senayasma at gmail.com> wrote:> Dear Admin, > I will appreciate if you advise me an effective way to write the following R > code including nested for loops. I cannot do it by using expand.grid > function because it results with memory allocation problems. > Thanks for your time and consideration. > > for(d1 in 0:n){ > for(d2 in 0:n){ > for(d3 in 0:n){ > for(d4 in 0:n){ > for(d5 in 0:n){ > for(d6 in 0:n){ > for(d7 in 0:n){ > for(d8 in 0:n){ > for(d9 in 0:n){ > for(d10 in 0:n){ > for(d11 in 0:n){ > for(d12 in 0:n){ > for(d13 in 0:n){ > for(d14 in 0:n){ > for(d15 in 0:n){ > for(d16 in 0:n){ > for(d17 in 0:n){ > for(d18 in 0:n){ > for(d19 in 0:n){ > for(d20 in 0:n){ > > list=c(d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20) > }}}}}}}}}}}}}}}}}}}} > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?