I have 'x' variables that I need to find the optimum combination of, with the constraint that the sum of all x variables needs to be exactly 100. I need to test all combinations to get the optimal mix.
This is easy if I know how many variables I have - I can hard code as below. But what if I don't know the number of variables and want this to be a flexible parameter. Is there a sexy recursive way that this can be done in R? #for combinations of 2 variables vars = 2 for(i in 0:100){ for(j in 0:(100-i)){ ...do some test i,j combination }} #for combinations of 3 variables vars = 3 for(i in 0:100){ for(j in 0:(100-i)){ for(k in 0:100-(i+j)){ ...do some test on i,j,k combination }}} -- View this message in context: http://r.789695.n4.nabble.com/can-this-sequence-be-generated-easier-tp3607240p3607240.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.