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! -- View this message in context: http://www.nabble.com/rbinom-for-a-matrix-tp18366867p18366867.html Sent from the R help mailing list archive at Nabble.com.
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!
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
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! 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/rbinom-for-a-matrix-tp18366867p18369250.html Sent from the R help mailing list archive at Nabble.com.