Hi,

Perhaps you can use expand.grid and then remove the mirror combinations,

values <- 1:3
tmp <- expand.grid(values, values)
unique.combs <- tmp[tmp[, 1]<=tmp[, 2], ]
unique.combs[do.call(order, unique.combs), ] # reorder if you wish
  Var1 Var2
1    1    1
4    1    2
7    1    3
5    2    2
8    2    3
9    3    3

I vaguely recall a discussion a few months ago on extending this approach to a variable number of arguments to expand.grid.

Hope this helps,

baptiste

On 11 Dec 2008, at 17:00, Reuben Cummings wrote:

Hi,

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
suggestions?

Reuben Cummings

______________________________________________
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.

_____________________________

Baptiste AuguiƩ

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

______________________________________________
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.

Reply via email to