Skip to content
Prev 227103 / 398500 Next

simple apply syntax

On Sun, 11 Jul 2010, Robin Jeffries wrote:

            
You don't want to use apply() here: rbinom is vectorized. However, you 
cannot use mat$x on a matrix, and the cbind() gave you a matrix 
anyway.  So something like

mat <- data.frame(p=runif(10,0,1), n=rep(1:5))
mat$x <- with(mat, rbinom(10, n, p))

is the idiomatic way to do it.

As an example of using apply and a matrix:

mat <- cbind(p=runif(10,0,1), n=rep(1:5))
x <- apply(mat, 1, function(z) rbinom(1, z['n'], z['p']))
mat <- cbind(mat, x=x)
Please so as we ask, and don't send HTML but rather properly formatted 
plain text (without all these blank lines).