Dear R-users, Is there an easy way to determine all possible vectors of length 21 with each entry having permutations from 0 to 4, instead of doing it like this? It really takes up too much time, and I am convinced that there exists something easier. Can you help me? Thank you in advance! Kind regards, Elke for (a in 0:4){ for (b in 0:4){ for (c in 0:4){ for (d in 0:4){ for (e in 0:4){ for (f in 0:4){ for (g in 0:4){ for (h in 0:4){ for (i in 0:4){ for (j in 0:4){ for (k in 0:4){ for (l in 0:4){ for (m in 0:4){ for (n in 0:4){ for (o in 0:4){ for (p in 0:4){ for (q in 0:4){ for (r in 0:4){ for (s in 0:4){ for (u in 0:4){ for (v in 0:4){ vec<-list(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,u,v) write.table(vec,file="C:/Documents and Settings/My Documents/permutations.txt",sep=";",col.names=FALSE, row.names=FALSE, append=TRUE) } } } } } } } } } } } } } } } } } } } } } ____________________________ Elke Moons, PhD Transportation Research Institute/ Instituut voor Mobiliteit (IMOB) Wetenschapspark 1, bus 15 3590 Diepenbeek BELGIUM Tel. +32-11-26.91.26 Fax. +32-11-26.91.99 E-mail: elke.moons@uhasselt.be [[alternative HTML version deleted]]
Dear Elke, I think you need something like this. It's only for 3 variables, but I think you know how to expand it for more variables. mat <- expand.grid(a = 0:4, b = 0:4, c = 0:4) write.table(mat, file="C:/Documents and Settings/My Documents/permutations.txt",sep=";",col.names=FALSE, row.names=FALSE) HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Elke Moons Verzonden: dinsdag 13 mei 2008 9:49 Aan: r-help op r-project.org Onderwerp: [R] Permutations Dear R-users, Is there an easy way to determine all possible vectors of length 21 with each entry having permutations from 0 to 4, instead of doing it like this? It really takes up too much time, and I am convinced that there exists something easier. Can you help me? Thank you in advance! Kind regards, Elke for (a in 0:4){ for (b in 0:4){ for (c in 0:4){ for (d in 0:4){ for (e in 0:4){ for (f in 0:4){ for (g in 0:4){ for (h in 0:4){ for (i in 0:4){ for (j in 0:4){ for (k in 0:4){ for (l in 0:4){ for (m in 0:4){ for (n in 0:4){ for (o in 0:4){ for (p in 0:4){ for (q in 0:4){ for (r in 0:4){ for (s in 0:4){ for (u in 0:4){ for (v in 0:4){ vec<-list(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,u,v) write.table(vec,file="C:/Documents and Settings/My Documents/permutations.txt",sep=";",col.names=FALSE, row.names=FALSE, append=TRUE) } } } } } } } } } } } } } } } } } } } } } ____________________________ Elke Moons, PhD Transportation Research Institute/ Instituut voor Mobiliteit (IMOB) Wetenschapspark 1, bus 15 3590 Diepenbeek BELGIUM Tel. +32-11-26.91.26 Fax. +32-11-26.91.99 E-mail: elke.moons op uhasselt.be [[alternative HTML version deleted]] ______________________________________________ R-help op 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.
Hi Elke, the matrix you are trying to create has 5^21 = 476837158203125 rows and 21 columns. I'm afraid Thierry's proposal with n=21 will not fit into memory. And the file you are writing is 5^21*5*8 bytes big, about 80108643 GB. Perhaps you want to think a little more about what you are trying to achieve and, e.g., consider random sampling from the matrix? In addition, look at your code: you append every single line to the output file, i.e., you open and close your file 5^21 = 476837158203125 times. File I/O always takes a lot of time. File I/O in a loop usually leads to slow execution. HTH, Stephan ONKELINX, Thierry schrieb:> Dear Elke, > > I think you need something like this. It's only for 3 variables, but I > think you know how to expand it for more variables. > > mat <- expand.grid(a = 0:4, b = 0:4, c = 0:4) > write.table(mat, file="C:/Documents and Settings/My > Documents/permutations.txt",sep=";",col.names=FALSE, row.names=FALSE) > > HTH, > > Thierry> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > Namens Elke Moons > Verzonden: dinsdag 13 mei 2008 9:49 > Aan: r-help at r-project.org > Onderwerp: [R] Permutations > > Dear R-users, > > > > > > Is there an easy way to determine all possible vectors of length 21 with > each entry having permutations from 0 to 4, instead of doing it like > this? > It really takes up too much time, and I am convinced that there exists > something easier. > > Can you help me? Thank you in advance! > > Kind regards, > > > > > > Elke > > > > for (a in 0:4){ > > for (b in 0:4){ > > for (c in 0:4){ > > for (d in 0:4){ > > for (e in 0:4){ > > for (f in 0:4){ > > for (g in 0:4){ > > for (h in 0:4){ > > for (i in 0:4){ > > for (j in 0:4){ > > for (k in 0:4){ > > for (l in 0:4){ > > for (m in 0:4){ > > for (n in 0:4){ > > for (o in 0:4){ > > for (p in 0:4){ > > for (q in 0:4){ > > for (r in 0:4){ > > for (s in 0:4){ > > for (u in 0:4){ > > for (v in 0:4){ > > vec<-list(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,u,v) > > write.table(vec,file="C:/Documents and Settings/My > Documents/permutations.txt",sep=";",col.names=FALSE, row.names=FALSE, > append=TRUE) > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > } > > }--