r - All combinations of all sizes? -


there thousands of results on when search "vector combinations in r" can't find answer question. apologies if duplicate:

i have vector (1,2,3,4) , want find combinations (n choose 2) (n choose n). in other words, vector want:

1,2,3,4 1,2,3 1,2,4 1,3,4 2,3,4 1,2 1,3 1,4 2,3 2,4 3,4 

and code generalizable once have larger vector, able generalize.

thanks!

if prefer compact code

map(combn, list(x), seq_along(x)) ## [[1]] ##      [,1] [,2] [,3] [,4] ## [1,]    1    2    3    4  ## [[2]] ##      [,1] [,2] [,3] [,4] [,5] [,6] ## [1,]    1    1    1    2    2    3 ## [2,]    2    3    4    3    4    4  ## [[3]] ##      [,1] [,2] [,3] [,4] ## [1,]    1    1    1    2 ## [2,]    2    2    3    3 ## [3,]    3    4    4    4  ## [[4]] ##      [,1] ## [1,]    1 ## [2,]    2 ## [3,]    3 ## [4,]    4 

to avoid repetition, you'll have deal nested list can simplify result using unlist

res <- map(combn, list(x), seq_along(x), simplify = false) unlist(res, recursive = false) ## [[1]] ## [1] 1  ## [[2]] ## [1] 2  ## [[3]] ## [1] 3  ## [[4]] ## [1] 4  ## [[5]] ## [1] 1 2  ## [[6]] ## [1] 1 3  ## [[7]] ## [1] 1 4  ## [[8]] ## [1] 2 3  ## [[9]] ## [1] 2 4  ## [[10]] ## [1] 3 4  ## [[11]] ## [1] 1 2 3  ## [[12]] ## [1] 1 2 4  ## [[13]] ## [1] 1 3 4  ## [[14]] ## [1] 2 3 4  ## [[15]] ## [1] 1 2 3 4 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -