soeren.vogel at eawag.ch
2009-Nov-25 09:11 UTC
[R] searching code for combination of vector
For a given numeric vector v of length n and sum s, is there a ready- to-run code that returns every combination of v in n summing up to s? Example for n=3 and s=2: v <- c(2, 0, 0) # find some coding here that returns [1] 2 0 0 [2] 1 1 0 [3] 1 0 1 [4] 0 2 0 [5] 0 1 1 [6] 0 0 2 Thanks S?ren -- S?ren Vogel, Dipl.-Psych. (Univ.), PhD-Student, Eawag, Dept. SIAM http://www.eawag.ch, http://sozmod.eawag.ch
soeren.vogel at eawag.ch wrote:> For a given numeric vector v of length n and sum s, is there a > ready-to-run code that returns every combination of v in n summing up to > s? Example for n=3 and s=2: > > v <- c(2, 0, 0) > # find some coding here that returns > [1] 2 0 0 > [2] 1 1 0 > [3] 1 0 1 > [4] 0 2 0 > [5] 0 1 1 > [6] 0 0 2Function comb() in pkg:forensim does just that. -Peter Ehlers> > Thanks > > S?ren >