[R] rbinom for a matrix

Dylan Beaudette dylan.beaudette at gmail.com
Wed Jul 9 21:42:42 CEST 2008

On Wednesday 09 July 2008, Ben Bolker wrote:
> ACroske <Audy3272 <at> yahoo.com> writes:
> > 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!
>   How about
> matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))
>   or (perhaps marginally more efficiently?)
> y <- (runif(m)<m)
> storage.mode(y) <- "double"
>   Ben Bolker

Wait a second. Are you trying to convert each probability into the most 
likely '1' or '0' through rounding? The code example above will give you a 
different answer every time you run it. Is that what you are looking for?

Just curious,


Dylan Beaudette
Soil Resource Laboratory
University of California at Davis

More information about the R-help mailing list