Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all combinations which sum up to 100 using R

Tags:

r

combinations

I need to get all combinations for which the sum equal 100 using 8 variables that could take any value from 0 to 100 by incremental step of 10. (i.e. 0, 10, 20 ... 100)

The following script does just that but is very inefficient as it creates a huge dataset and I was wondering if someone had a better way of doing this.

x <- expand.grid("ON" = seq (0,100,10), 
        "3M" = seq(0,100,10), 
        "6M" = seq(0,100,10), 
        "1Y" = seq(0,100,10), 
        "2Y" = seq(0,100,10),
        "5Y" = seq(0,100,10), 
        "10Y" = seq(0,100,10), 
        "15Y" = seq(0,100,10))

x <- x[rowSums(x)==100,]

Edit --

to answer the question from Stéphane Laurent

the result should look like

ON 3M 6M 1Y 2Y 5Y 10Y 15Y        
100 0  0  0  0  0   0   0  
 90 10  0  0  0  0   0   0  
 80 20  0  0  0  0   0   0  
 70 30  0  0  0  0   0   0  
 60 40  0  0  0  0   0   0  
 50 50  0  0  0  0   0   0

(...)

  0 0  0  0  0  0 10  90  
  0 0  0  0  0  0  0 100
like image 591
ulrich Avatar asked Mar 06 '14 08:03

ulrich


People also ask

How do you find the number of combinations in r?

Combinations. The number of possible combinations is C(n,r)=n! r! (n−r)!

How do you find the sum of all combinations?

The sum of all possible combinations of n distinct things is 2 n. C0 + nC1 + nC2 + . . . + nC n = 2 n.

How do you make all combinations of a vector in r?

To create combination of multiple vectors, we can use expand. grid function. For example, if we have six vectors say x, y, z, a, b, and c then the combination of vectors can be created by using the command expand. grid(x,y,z,a,b,c).

What is r in combination and permutation?

Combinat package in R programming language can be used to calculate permutations and combinations of the numbers. It provides routines and methods to perform combinatorics. combn() method in R language belonging to this package is used to generate all combinations of the elements of x taken m at a time.


1 Answers

followed by Stéphane Laurent's answer, I am able to get a super fast solution by using the uniqueperm2 function here.

library(partitions)

C = t(restrictedparts(10,8))
do.call(rbind, lapply(1:nrow(C),function(i)uniqueperm2(C[i,])))

Update, there is faster solution using iterpc the package.

library(partitions)
library(iterpc)
C = t(restrictedparts(10,8))
do.call(rbind, lapply(1:nrow(C),function(i) getall(iterpc(table(C[i,]), order=T))))

It is about twice the speed of the uniqueperm2

> f <- function(){
    do.call(rbind, lapply(1:nrow(C),function(i)uniqueperm2(C[i,])))
}
> g <- function(){
    do.call(rbind, lapply(1:nrow(C),function(i) getall(iterpc(table(C[i,]), order=T))))
}
> microbenchmark(f(),g())
Unit: milliseconds
 expr      min       lq     mean   median       uq      max neval cld
  f() 36.37215 38.04941 40.43063 40.07220 42.29389 46.92574   100   b
  g() 16.77462 17.45665 19.46206 18.10101 20.65524 64.11858   100  a 
like image 90
Randy Lai Avatar answered Sep 29 '22 12:09

Randy Lai