R:Binary matrix for all possible unique results

后端 未结 3 1430
孤独总比滥情好
孤独总比滥情好 2020-12-18 06:02

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 u

3条回答
  •  旧时难觅i
    2020-12-18 06:43

    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
    

提交回复
热议问题