How to generate a binary matrix for all possible permutations of 'i' variables X, where " i " can be any number between 1 and infinite. Resultant matrix will have 2^ i unique rows.
For i=2 , variables x1, x2 each with a possible value of 1 or 0, so the resultant matrix would be:
X1 X2
0 0
0 1
1 0
1 1
Is there any function in R to generate ?
I tried with below function:
matrix(rbinom(160, 1, 0.5),ncol=5,nrow=(2^5))
But the result does not show all possible values.
The fonction combos of the package hier.part will do the job I think.
Except that you will have to add the "null" combination. HTH
you can use
expand.grid
:More generally, with 5 columns for example, giving
m
:Try this