Kok Lin
Kok Lin

Reputation: 39

Arrange different data set using matrix code

I'm trying to use repeat loop to generate 100 data set of Poisson Distribution with sample size n=100 and I would like to arrange the result in by row and column but it is just show me repeating to show me the last set of data while not all the data set. At the same time I would also trying to figure out the way to get the mean, variance and MSE of the 100 data set.

set.seed(124)


a <- 1
repeat{
b = rpois(100, lambda = 3)
Storage100 <- matrix(data=b,nrow=100,ncol=1)
a = a+1
print(b)
if (a>100){break
  }
}
Storage100

I'm expecting that my 100 data set can be show like first set of data in first column, second set of data in second column.....

Upvotes: 1

Views: 33

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388962

Use replicate with simplify as TRUE to get matrix of dimension 100 X 100 where each column represents the distribution.

set.seed(124)
m1 <- replicate(100, matrix(data=rpois(100, lambda = 3),ncol = 1), simplify = TRUE)

To get the mean for each column we can use colMeans (thanks to @jay.sf)

colMeans(m1)

Upvotes: 1

Related Questions