Re: [R] rbinom for a matrix

From: Ben Bolker <>
Date: Thu, 10 Jul 2008 13:58:15 +0000 (UTC)

ACroske <Audy3272 <at>> writes:

> Ben:
> Thanks for the reply. One further question, and this is where my novice
> status at R shows through. The code makes sense, but what would I put it for
> "m"? Is it the same number for all three (that was my first thought since it
> was the same placeholder for all three). Number of rows in the matrix is 56.
> There are 2576 total cells (observations). Each cell has its own
> probability.
> Thanks for your help!
> >
> > matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))
> >
> > or (perhaps marginally more efficiently?)
> >
> > y <- (runif(m)<m)
> > storage.mode(y) <- "double"
> >

  m is your matrix of probabilities; for example, you might do

 m = as.matrix(read.table("mystuff.txt"))

(or import it from ArcGIS, or whatever).

length(m) will be computed automatically (=2576), as will nrow(m) (=56).

  Erik Iverson's solution should work too, but I think mine will be much more efficient -- picking a whole slug of random numbers at once is much faster than looping and picking them one at a time.

  Ben Bolker mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 10 Jul 2008 - 14:10:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Jul 2008 - 14:31:19 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive