Re: [R] Generate Binary Matrix

2014-04-09 Thread David Carlson
You could randomly assign 1 to a single column in each row and then use binomial draws on the remaining 0's: > set.seed(42) > dimMat <- matrix(0, 1000, 4) > dimMat[cbind(1:1000, sample.int(4, 1000, replace=TRUE))] <- 1 > dimMat[dimMat<1] <- sample(0:1, 3000, replace=TRUE, prob=c(.6, .4)) > table(r

Re: [R] Generate Binary Matrix

2014-04-09 Thread Clint Bowman
A bit kludgey but how about: dimMat <- matrix(0, 1000, 4) for(i in 1:1000){ while(sum(dimMat[i, ] <- sample(c(0,1), 4, replace = TRUE, prob = c(.3, .7)))==0) dimMat[i, ] <- sample(c(0,1), 4, replace = TRUE, prob = c(.3, .7)) } table(rowSums(dimMat)) Clint BowmanINTERNET:

Re: [R] Generate Binary Matrix

2014-04-09 Thread ken knoblauch
Doran, Harold air.org> writes: > I am trying to generate a binary matrix where row in the matrix is guaranteed to have at least one 1. > Ideally, I would like most rowSums to be equal 2 or 3 with some 1s and some 4s. But, rowSums cannot be equal > to 0. > > I can tinker with the vector o