| By David Smith | Article Rating: |
|
| September 4, 2012 07:37 PM EDT | Reads: |
496 |
It all started off as a simple question from Scott Chamberlain on Twitter:
Make m x n matrix with randomly assigned 0/1 -> apply(m, c(1,2), function(x) sample(c(0,1),1)) -- Better/faster solution? #rstats
— Scott Chamberlain (@recology_) August 28, 2012
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 Read the original blog entry...
Published September 4, 2012 Reads 496
Copyright © 2012 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By David Smith
David Smith is Vice President of Marketing and Community at Revolution Analytics. He has a long history with the R and statistics communities. After graduating with a degree in Statistics from the University of Adelaide, South Australia, he spent four years researching statistical methodology at Lancaster University in the United Kingdom, where he also developed a number of packages for the S-PLUS statistical modeling environment. He continued his association with S-PLUS at Insightful (now TIBCO Spotfire) overseeing the product management of S-PLUS and other statistical and data mining products.< David smith is the co-author (with Bill Venables) of the popular tutorial manual, An Introduction to R, and one of the originating developers of the ESS: Emacs Speaks Statistics project. Today, he leads marketing for REvolution R, supports R communities worldwide, and is responsible for the Revolutions blog. Prior to joining Revolution Analytics, he served as vice president of product management at Zynchros, Inc. Follow him on twitter at @RevoDavid

