[R] Combinatorial problem

David Scott d.scott at auckland.ac.nz
Wed Aug 12 13:32:24 CEST 2009

I have been struggling trying to write some code to produce all 
combinations subject to some restrictions. I thought someone might have 
some bright ideas.

I have 11 values which fall into 5 groups. I want all combinations of 
2,3, and 4 values where each value must be from a different group. The 
numbers in the groups are different. Here is a definition of the groups:

groups <- list(gp1 = 1:3, gp2 = 4:5, gp3 = 6:7,
                gp4 = 8:10, gp5 = 11)

I don't need the problem to be generalized at all---I just want to solve 
this problem.

My approach so far has been to use combinations() from gtools to select 
the combinations of groups and then to try and expand from there. I am 
nearly there I think but it seems excessively complicated. I can 
definitely do the all combinations of 2 from 11 subject to the group 

Suggestions are most welcome.

David Scott

David Scott	Department of Statistics
		The University of Auckland, PB 92019
		Auckland 1142,    NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email:	d.scott at auckland.ac.nz,  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics

More information about the R-help mailing list