Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate all possible permutations (or n-tuples)

Tags:

r

I'd like to create a data.frame of all possible permutations of 10 variables that can be either 1 or 2

2*2*2*2*2*2*2*2*2*2 = 1024 # possible

1,1,1,1,1,1,1,1,1,1
1,2,1,1,1,1,1,1,1,1
1,2,2,1,1,1,1,1,1,1
1,2,2,2,1,1,1,1,1,1
...

Is there a "quick" way to do this in R?

like image 787
Brandon Bertelsen Avatar asked Feb 23 '12 23:02

Brandon Bertelsen


1 Answers

how about this:

tmp = expand.grid(1:2,1:2,1:2,1:2,1:2,1:2,1:2,1:2,1:2,1:2)

or this (thanks Tyler):

x <- list(1:2)
tmp = expand.grid(rep(x, 10))
like image 72
baha-kev Avatar answered Oct 16 '22 21:10

baha-kev