Re: [R] all combinations with replacement

From: William Dunlap <wdunlap_at_tibco.com>
Date: Fri, 22 Apr 2011 07:44:20 -0700

Does the following do what you want? It should generate all the (unordered) NPart-partitions of Sum by mapping the output of combn(Sum+NParts-1,NParts-1).

f <- function (Sum, NParts)
{

    cm <- combn(Sum + NParts - 1, NParts - 1)     cm <- rbind(cm, Sum + NParts)
    if (NParts > 1) {

        r <- 2:NParts
        cm[r, ] <- cm[r, ] - cm[r - 1, ]
    }
    t(cm - 1)
}

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Kehl Dániel
> Sent: Thursday, April 21, 2011 12:29 PM
> To: r-help_at_r-project.org
> Subject: [R] all combinations with replacement
>
> Dear all,
>
> is there an easy way to get all possible combinations (?)
> with replacement.
> If n=6, k=3, i want something like
>
> 0 0 6
> 0 5 1
> 0 4 2
> 0 3 3
> 0 2 4
> .
> .
> .
> 5 0 1
> 5 1 0
> 6 0 0
>
> I tried to look at combn() but I could not get this done with it.
>
> Thank you in advance:
> Daniel
>
> ______________________________________________
> 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.
>



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. Received on Fri 22 Apr 2011 - 14:48:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 22 Apr 2011 - 14:50:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive