How to create random binary matrices in R

September 4, 2012

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

It all started off as a simple question from Scott Chamberlain on Twitter:

The goal was to create a matrix with randomly selected binary elements, and a predetermined number of rows and columns, that looks something like this:

     [,1] [,2] [,3] [,4]
[1,]    0    1    1    0
[2,]    0    0    0    1
[3,]    1    0    1    1

Many suggestions followed (including one from me). There were several different ways suggested of creating the random binary values:

  • Use the runif function to create random numbers between 0 and 1, and round to the nearest whole number.
  • Use ifelse on the output of runif, and assign 0 if it's below 0.5, and 1 otherwise.
  • Use the rbinom function to sample from a binomial distribution with a size of 1 and probability 0.5
  • Use the sample function with the replace=TRUE option to simulate selections of 0 and 1.

There were also different ways suggested for generating the matrix:

  • Use a for loop to fill each element of the matrix individually.
  • Generate random numbers row by row, and fill the matrix using apply.
  • Generate all the random numbers at once, and use the "matrix" function to create the matrix directly.

Luis Apiolaza reviews the suggested methods. Each has its merits: in clarity of code, in elegance, and especially in performance. On that front, Dirk Eddelbuettel benchmarked several of the solutions, including translating the code into C++ using Rcpp. One surprising outcome: translating the problem into C++ is only somewhat faster than using one call to sample. As Dirk says, this shows that "well-written R code can be competitive" with machine code. 

Thinking inside the Box: Faster creation of binomial matrices 

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)