> n_toss = 3; # Number of Tosses
> n_trial = 10; # Number of Trials
>
> mySamples <- sample(c(1,0), n_toss, replace = T, prob=c(0.5,0.5))
> mySamples
[1] 1 0 1
>
> mySamples <- replicate(n_trial,
+ {
+ mySamples <- sample(c(1,0), n_toss, replace = T, prob=c(0.5,0.5))
+ })
> mySamples
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 1 0 1 0 0 0 0 0
[2,] 1 0 1 1 1 0 0 0 0 0
[3,] 0 0 0 0 1 1 1 1 0 1
>
I have two questions here:
- Why does
mySamples
return a matrix? Shouldn't it return the last (10th) vector as R keeps the last statement being evaluated? - Is it possible to keep
mySamples <- sample(c(1,0), n_toss, replace = T, prob=c(0.5,0.5))
this statement in a separate function and still obtain a same result?