[R] generate combination multiset (set with repetition)

G. Jay Kerns gkerns at ysu.edu
Thu Dec 11 19:23:07 CET 2008


Dear Reuben,


On Thu, Dec 11, 2008 at 12:53 PM, baptiste auguie <ba208 at exeter.ac.uk> wrote:
> 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
>


Here is another way:

library(prob)
urnsamples(1:3, size = 2, ordered = FALSE, replace = TRUE)

You can convert to a matrix with as.matrix(), if desired.

Regards,
Jay


-- 

***************************************************
G. Jay Kerns, Ph.D.
Associate Professor
Department of Mathematics & Statistics
Youngstown State University
Youngstown, OH 44555-0002 USA
Office: 1035 Cushwa Hall
Phone: (330) 941-3310 Office (voice mail)
-3302 Department
-3170 FAX
E-mail: gkerns at ysu.edu
http://www.cc.ysu.edu/~gjkerns/



More information about the R-help mailing list