R Question

Make a matrix of two variables each row from replicating binomial distribution proportion

So... I have some issue...

Using R, I'm trying to simulate a Binomial distribution with n=10 and p=0.6, with size 1, and then, getting how many 1s and 0s I get, and this simulation should be repeated 200 times, so as to take a matrix of 200 rows and two columns with the number of 1s in the first column and the number of 0s in the second column.

I defined a function so as:

binom10.2 <- function(i){
x <- rbinom(10,1,0.6)
y <- as.vector(table(x))
return(y)
}


which gives me a result such as:

> binom10.2(i)
[1] 5 5
> binom10.2(i)
[1] 7 3
> binom10.2(i)
[1] 5 5
> binom10.2(i)
[1] 4 6


I need this result, replicated 200 times, in a matrix, like this:

[,1] [,2]
[1,] 1 9
[2,] 7 3
[3,] 5 5
[4,] 4 6
[5,] 2 8
[6,] 5 5
[7,] 4 6
[8,] 4 6
[9,] 5 5
[10,] 3 7
[11,] 1 9
[12,] 3 7
[13,] 4 6
[14,] 5 5
[15,] 2 8


The thing is, that I wrote this code so as to get it:

proportions10 <- t(sapply(1:200, binom10.2)
proportions10 <- t(replicate(200, binom10.2(i)))


I think both ways are the same, BUT, when running both of them, I sometimes get the desired matrix, and sometimes get a list, like this:

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39] [,40]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,61] [,62] [,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,71] [,72] [,73] [,74] [,75] [,76] [,77] [,78] [,79] [,80]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,81] [,82] [,83] [,84] [,85] [,86] [,87] [,88] [,89] [,90]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98] [,99] [,100]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,101] [,102] [,103] [,104] [,105] [,106] [,107] [,108] [,109] [,110]
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
[,111] [,112] [,113] [,114] [,115] [,116] [,117] [,118] [,119] [,120]


For me it has no sense that sometimes, depending on how many times I run the code, I get the first desired matrix, and some other times, I get the second list, useless. I would like the first matrix to always appear no matter how many times I run the code...

Could anyone help?

Answer

Sums of the 1's show the number of successes in each replication. subtracting that number from 10 gives the number failures.

Here is my way:

 x = replicate(200, expr={sum(rbinom(10, 1, .6))})
 m = matrix(0, nrow=200, ncol=2)
 m[,2]=x
 m[,1]=10-x