Making matrices with zeros and ones

So I was trying to figure out a fast way to make matrices with randomly allocated 0 or 1 in each cell of the matrix. I reached out on Twitter, and got many responses (thanks tweeps!). Here is the solution I came up with. See if you can tell why it would be slow. {% highlight r linenos %} mm <- matrix(0, 10, 5) apply(mm, c(1, 2), function(x) sample(c(0, 1), 1)) {% endhighlight %}...

August 30, 2012 · 4 min · Scott Chamberlain