I have a large data.table
and I am trying to generate binomial random numbers (using rbinom
) using the values of one of the columns as the parameter to the distribution. Assume that index
is a unique row identifier, and that the parameter is in the responseProb
column. Then
dt[, response := rbinom(1, 1, responseProb), by = index]
rbinom
's signature is rbinom(n, size, prob)
, but since it is not vectorized over the prob
argument, it can only take a scalar as input, so I can't, but would be able to write:
dt[, response := rbinom(1, 1, responseProb)]
To give a simple example of what I mean, rbinom(1, 1, seq(0.1, 0.9, .1))
, yields
> rbinom(1, 1, seq(0.1, 0.9, .1))
[1] 1
I think that the solution to this is to use
dt[, response := rbinom(probResponse, 1, responseProb)]
but want to double check that this would lead to the same answer as the first line of code.