Does anyone know of a function that will return all unordered combinations of n elements from a list with repetition? The combs function in caTools will do this without repetition: combs(1:2, 2) [,1] [,2] [1,] 1 2 What I'd like is: 1 1 1 2 2 2 Thank you, Thomas Chesney This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham. This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
You could try expand.grid -- you'd prob need to modify what's beneath *a=c(0,1,2)* *b=c(0,1)* *c=c(0,1)* *y<-list()* *y[[1]]<-a* *y[[2]]<-b* *y[[3]]<-c* *expand.grid(y)* This code gives all combinations On 9 June 2015 at 10:11, Thomas Chesney <Thomas.Chesney at nottingham.ac.uk> wrote:> Does anyone know of a function that will return all unordered combinations > of n elements from a list with repetition? > > The combs function in caTools will do this without repetition: > > combs(1:2, 2) > > [,1] [,2] > [1,] 1 2 > > What I'd like is: > > 1 1 > 1 2 > 2 2 > > Thank you, > > Thomas Chesney > > > > This message and any attachment are intended solely for the addressee > and may contain confidential information. If you have received this > message in error, please send it back to me, and immediately delete it. > > Please do not use, copy or disclose the information contained in this > message or in any attachment. Any views or opinions expressed by the > author of this email do not necessarily reflect the views of the > University of Nottingham. > > This message has been checked for viruses but the contents of an > attachment may still contain software viruses which could damage your > computer system, you are advised to perform your own checks. Email > communications with the University of Nottingham may be monitored as > permitted by UK legislation. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Thank you Nicholas. I've found that Urnsamples in the prob package does it too: urnsamples(1:2, size = 2, replace = TRUE, ordered = FALSE) Thomas ________________________________________ From: WRAY NICHOLAS [nicholas.wray at ntlworld.com] Sent: Tuesday, June 09, 2015 10:52 AM To: Thomas Chesney Cc: r-help at r-project.org Subject: Re: [R] Unordered combinations with repetition You could try expand.grid -- you'd prob need to modify what's beneath a=c(0,1,2) b=c(0,1) c=c(0,1) y<-list() y[[1]]<-a y[[2]]<-b y[[3]]<-c expand.grid(y) This code gives all combinations On 9 June 2015 at 10:11, Thomas Chesney <Thomas.Chesney at nottingham.ac.uk<mailto:Thomas.Chesney at nottingham.ac.uk>> wrote: Does anyone know of a function that will return all unordered combinations of n elements from a list with repetition? The combs function in caTools will do this without repetition: combs(1:2, 2) [,1] [,2] [1,] 1 2 What I'd like is: 1 1 1 2 2 2 Thank you, Thomas Chesney This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham. This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation. ______________________________________________ R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see 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. This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham. This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
> combnWithRepetition <- function(n, k) combn(n+k-1, k) - seq(from=0, len=k) > combnWithRepetition(2, 2)[,1] [,2] [,3] [1,] 1 1 2 [2,] 1 2 2> combnWithRepetition(3, 2)[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 2 2 3 [2,] 1 2 3 2 3 3 Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Jun 9, 2015 at 2:11 AM, Thomas Chesney < Thomas.Chesney at nottingham.ac.uk> wrote:> Does anyone know of a function that will return all unordered combinations > of n elements from a list with repetition? > > The combs function in caTools will do this without repetition: > > combs(1:2, 2) > > [,1] [,2] > [1,] 1 2 > > What I'd like is: > > 1 1 > 1 2 > 2 2 > > Thank you, > > Thomas Chesney > > > > This message and any attachment are intended solely for the addressee > and may contain confidential information. If you have received this > message in error, please send it back to me, and immediately delete it. > > Please do not use, copy or disclose the information contained in this > message or in any attachment. Any views or opinions expressed by the > author of this email do not necessarily reflect the views of the > University of Nottingham. > > This message has been checked for viruses but the contents of an > attachment may still contain software viruses which could damage your > computer system, you are advised to perform your own checks. Email > communications with the University of Nottingham may be monitored as > permitted by UK legislation. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
That combnWithRepetition (based on combn) can use much less memory (and time) than the algorithm in prob:::urnsamples.default with replace=TRUE, ordered=FALSE. Perhaps urnsamples() could be updated to use combn instead of unique(as.matrix(expand.grid())). See the urn chapter in Feller vol. 1. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Jun 9, 2015 at 8:56 AM, William Dunlap <wdunlap at tibco.com> wrote:> > combnWithRepetition <- function(n, k) combn(n+k-1, k) - seq(from=0, > len=k) > > combnWithRepetition(2, 2) > [,1] [,2] [,3] > [1,] 1 1 2 > [2,] 1 2 2 > > combnWithRepetition(3, 2) > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 1 1 1 2 2 3 > [2,] 1 2 3 2 3 3 > > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Tue, Jun 9, 2015 at 2:11 AM, Thomas Chesney < > Thomas.Chesney at nottingham.ac.uk> wrote: > >> Does anyone know of a function that will return all unordered >> combinations of n elements from a list with repetition? >> >> The combs function in caTools will do this without repetition: >> >> combs(1:2, 2) >> >> [,1] [,2] >> [1,] 1 2 >> >> What I'd like is: >> >> 1 1 >> 1 2 >> 2 2 >> >> Thank you, >> >> Thomas Chesney >> >> >> >> This message and any attachment are intended solely for the addressee >> and may contain confidential information. If you have received this >> message in error, please send it back to me, and immediately delete it. >> >> Please do not use, copy or disclose the information contained in this >> message or in any attachment. Any views or opinions expressed by the >> author of this email do not necessarily reflect the views of the >> University of Nottingham. >> >> This message has been checked for viruses but the contents of an >> attachment may still contain software viruses which could damage your >> computer system, you are advised to perform your own checks. Email >> communications with the University of Nottingham may be monitored as >> permitted by UK legislation. >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >> > >[[alternative HTML version deleted]]