R:Binary matrix for all possible unique results

邮差的信 提交于 2019-11-29 07:24:41

you can use expand.grid:

 expand.grid(c(0,1),c(0,1))
  Var1 Var2
1    0    0
2    1    0
3    0    1
4    1    1

More generally, with 5 columns for example, giving m:

m <- as.data.frame(matrix(rbinom(5*2, 1, 0.5),ncol=5))
 V1 V2 V3 V4 V5
1  0  1  1  0  0
2  0  1  1  0  0

dim(expand.grid(m))
32 5

The fonction combos of the package hier.part will do the job I think.

require(hier.part)
combos(2)$binary
     [,1] [,2]
[1,]    1    0
[2,]    0    1
[3,]    1    1

combos(3)$binary
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1
[4,]    1    1    0
[5,]    1    0    1
[6,]    0    1    1
[7,]    1    1    1

Except that you will have to add the "null" combination. HTH

Try this

i =2
install.packages('gtools')
library(gtools)
permutations(2,i,v=c(0,1),repeats.allowed=TRUE)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!