Reuben Cummings
2008-Dec-11 17:00 UTC
[R] generate combination multiset (set with repetition)
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
baptiste auguie
2008-Dec-11 17:53 UTC
[R] generate combination multiset (set with repetition)
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 3I 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 at 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
G. Jay Kerns
2008-Dec-11 18:23 UTC
[R] generate combination multiset (set with repetition)
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/
Charles C. Berry
2008-Dec-11 18:26 UTC
[R] generate combination multiset (set with repetition)
On Thu, 11 Dec 2008, 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)Why wrap 1:3 in array() ??> > 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 >For small problems (n < 100, say) : which( lower.tri( diag( n ), diag=TRUE), arr.ind=TRUE )[,2:1] For larger problems, something like : foo <- function(n) { brks <- cumsum( n:1 ) k <- 1:choose( n+1, 2 ) j <- findInterval( k, brks+1 ) + 1 i <- k - ( brks-brks[1] )[ j ] cbind( j, i ) } If the number in 'set' are not 1:n, you can do a lookup using the results from above. HTH, Chuck> 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 at 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901