I want to generate an nxm matrix. Suppose its 100x3. I want each row to sum to 1 (so two \"0\"\'s and one \"1\").
sample(c(0,0,1),3)
will
mat <- matrix(runif(300),ncol=3) mat[] <- as.numeric(t(apply(mat, 1, function(r) r == max(r))))