I am trying to create a matrix of 0s, 1s and 2s. Let's call this dataset A such that A[i,j] ~ Binomial(2, P[i,j]), where P is another matrix that gives the probabilities of each entry in A. So, each entry in the matrix A will be binomially distributed according to its corresponding probability entry in matrix P. The following gives a for-loop that illustrates what I want but this is really slow in R, so I was thinking if anyone knows how I can do it with the apply function? Both P and A are m*n matrices.
for (i in 1:m) {
  for (j in 1:n) {
    a[i,j] = rbinom(n = 1, size = 2, prob = p[i,j])
  }
}
 
    