Peter Jaksons
2011-Jan-27 23:29 UTC
[R] subloop - flexible coding - variable number of loops
Hello, I want to create a flexible code for the following example: In stead of using a different code for each n (as in my example below), I want to write a general code for every n (n from 1 to 10). I tried a lot of things, but I always got stuck with the number of subloops or witch closing the brackets. Any suggestions? Cheers, Peter Department of Mathematics and Statistics University of Canterbury New Zealand n <-2 matrixallo <- matrix(nrow=0,ncol=n) for(i in 1:20) { for (j in 1:20) { if (i+j == 20) { newentry<- cbind(i,j) matrixallo <- rbind(matrixallo,newentry) } } } n <-3 matrixallo <- matrix(nrow=0,ncol=n) for(i in 1:20) { for (j in 1:20) { for (k in 1:20) { if (i+k+j == 20) { newentry<- cbind(i,j,k) matrixallo <- rbind(matrixallo,newentry) } } } } This email may be confidential and subject to legal privilege, it may not reflect the views of the University of Canterbury, and it is not guaranteed to be virus free. If you are not an intended recipient, please notify the sender immediately and erase all copies of the message and any attachments. Please refer to http://www.canterbury.ac.nz/emaildisclaimer for more information. [[alternative HTML version deleted]]
David Winsemius
2011-Jan-28 06:14 UTC
[R] subloop - flexible coding - variable number of loops
On Jan 27, 2011, at 6:29 PM, Peter Jaksons wrote:> Hello, > > > > I want to create a flexible code for the following example: In stead > of > using a different code for each n (as in my example below), I want to > write a general code for every n (n from 1 to 10).Have you done any thinking about the size of hte programming task? Perhaps running this code? > 20^(1:10) [1] 2.000e+01 4.000e+02 8.000e+03 1.600e+05 3.200e+06 6.400e+07 1.280e+09 2.560e+10 5.120e+11 1.024e+13 So you might want to think twice about trying for n > 6 right off the bat/ expand.grid could be used: col3 <- expand.grid(as.data.frame(matrix(rep(1:20, 3), ncol=3))) col3[apply(col3, 1,sum)==20, ] > nrow( col3[apply(col3, 1,sum)==20, ] ) [1] 171 -- David.> > I tried a lot of things, but I always got stuck with the number of > subloops or witch closing the brackets. Any suggestions? > > > > Cheers, > > > > Peter > > > > Department of Mathematics and Statistics > > University of Canterbury > > New Zealand > > > > > > n <-2 > > > > matrixallo <- matrix(nrow=0,ncol=n) > > > > for(i in 1:20) > > { > > for (j in 1:20) > > { > > if (i+j == 20) > > { > > newentry<- cbind(i,j) > > matrixallo <- rbind(matrixallo,newentry) > > } > > } > > } > > > > > > n <-3 > > > > matrixallo <- matrix(nrow=0,ncol=n) > > > > for(i in 1:20) > > { > > for (j in 1:20) > > { > > for (k in 1:20) > > { > > if (i+k+j == 20) > > { > > newentry<- cbind(i,j,k) > > matrixallo <- rbind(matrixallo,newentry) > > } > > } > > } > > } > > > This email may be confidential and subject to legal privilege, it may > not reflect the views of the University of Canterbury, and it is not > guaranteed to be virus free. If you are not an intended recipient, > please notify the sender immediately and erase all copies of the > message > and any attachments. > > Please refer to http://www.canterbury.ac.nz/emaildisclaimer for more > information. > > [[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.David Winsemius, MD West Hartford, CT