# [R] How to repeat a procedure

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Wed Feb 18 18:23:11 CET 2004

```Haiyan Chen wrote:

>Hello,
>
>1. After I generate a 100x50 matrix by x3<-matrix(0,100,50);for (i in
>1:100) {x1<-rpois(50, mu[i]);x2<-x1; x2[runif(50)<.01]<-0; x3[i,]<-x2},
>
>
>2. I want to calculate means and sample variances of each row and create a
>new matrix 100x2;
>
Try:

mean.var<-function(n=10, m=5){
mu<-(1:n)^2
x<-matrix(rpois(n*m,mu),n,m)
stat<-t(apply(x,1,function(x)c(mean(x),var(x))))
}

to set some elements of x to zero you can add an additional line in mean.var

>3. I then want to repeat above procedure 500 times so that eventually I
>will have 500 100x2 matrices.
>
n<-100; m<-50
n.comp<-500
for(i in 1:n.comp)
eval(parse(text=paste("result",i,"<-mean.var(n,m)",sep="")))

Peter

>Would someone mind helping me to code 2 & 3?
>
>
>Heyen
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help