1

How can I generate a binary matrix that has the following behavior? I have came across this solution but it lacks the random positioning of the binary values.

## all rows contain four 1's at random column

       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    1    1    0    0    0    0    0    1     0
 [2,]    0    0    0    1    1    1    0    0    0     1
 [3,]    0    0    0    1    1    0    0    1    1     0
 [4,]    0    1    1    0    0    0    1    0    0     1
 [5,]    0    1    0    1    0    1    0    1    0     0
Community
  • 1
  • 1
jacky_learns_to_code
  • 824
  • 3
  • 11
  • 29

1 Answers1

1

This might not be optimized but will get you going:

m<-matrix(data = rep(sample(rep(c(1,0),times=c(4,6))),times=5),byrow = T,nrow = 5,ncol = 10)
m<-apply(X = m,FUN = sample,MARGIN = 1)
m<-t(m)
tushaR
  • 3,083
  • 1
  • 20
  • 33