[R] generate combination multiset (set with repetition)

Reuben Cummings reubano at gmail.com
Thu Dec 11 18:00:30 CET 2008


This has been asked before but not sufficiently answered from what I
could find. How do you create combinations
with repetitions (multisets) in R?

If I have
> set <- array(1:3)

And I want to choose all combinations of picking 2 numbers, I want to
get a print out like

     [,1] [,2]
[1,]    1    1
[2,]    1    2
[3,]    1    3
[4,]    2    2
[5,]    2    3
[6,]    3    3

subsets(set, 2, allow.repeat=T) should work, but I can't get the
multic package to install, t(combn(set,2)) was suggested but it
doesn't produce repetitions; expand.grid(rep(list(1:3), 2)) was also
suggested, but it produces permuations, not combinations.
Additionally, I would like to iterate through each resultant set for
large n (similar to the description for getNextSet {pcalg}). Any

Reuben Cummings

More information about the R-help mailing list