antonis34
antonis34

Reputation: 41

Generate 100 samples for each i of a loop in R

I generated 10 Bernoulli samples of size 2000 each one. I used this command:

# Data
samples<-matrix(NA, nrow = 10, ncol = 2000)
for (i in 1:10) {
  samples[i,] <- rbinom(2000, 1, prob = 0.05)
}

But now I want to generate 100 Bernoulli samples for each i of the loop. All these samples have to be stored at a matrix. I tried this command but I am doing something wrong with me matrix:

for (j in 1:100) {
  samples[j,] <- matrix(NA, nrow = 10, ncol = 2000)
  for (i in 1:10) {
    samples[i,] <- replicate(100, rbinom(2000, 1, prob = 0.05))
  }
}

Thanks for your help

Upvotes: 0

Views: 298

Answers (1)

user2974951
user2974951

Reputation: 10385

Or better yet

matrix(rbinom(10*2000,1,prob=0.05),nrow=10,ncol=2000)

to generate 2000 cases for each sample, 10 samples in this case.

Upvotes: 1

Related Questions