Skip to content
Prev 257737 / 398502 Next

all combinations with replacement

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