[R] rbinom for a matrix

Erik Iverson iverson at biostat.wisc.edu
Wed Jul 9 20:42:54 CEST 2008


Is this what you're looking for?

test <- matrix(runif(100, 0, 1), nrow = 20)
nr <- nrow(test)
matrix(sapply(test, rbinom, n = 1, size = 1), nrow = nr)

ACroske wrote:
> I have a large matrix full of probabilities; I would like to convert each
> probability to a 1 or a 0 using rbinom.
> How can I do this on the entire matrix? The matrix was converted from a
> raster ArcMap dataset, so the matrix is essentially a map. Because of this,
> I have no column headings.
> Thanks!



More information about the R-help mailing list