Skip to content
Back to formatted view

Raw Message

Message-ID: <77EB52C6DD32BA4D87471DCD70C8D7000423576D@NA-PA-VBE03.na.tibco.com>
Date: 2011-04-22T14:44:20Z
From: William Dunlap
Subject: all combinations with replacement
In-Reply-To: <4DB08569.9080605@ktk.pte.hu>

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