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; 3. I then want to repeat above procedure 500 times so that eventually I will have 500 100x2 matrices. Would someone mind helping me to code 2 & 3? Thanks ahead of time. Heyen
On Wed, 18 Feb 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},YOu can do this without the loop, eg x3<-rpois(50*100, rep(mu,each=100)) x3<-ifelse(runif(50*100)<0.01, 0, x3) x3<-matrix(x3, ncol=50)> 2. I want to calculate means and sample variances of each row and create a > new matrix 100x2;means<-apply(x3,2,mean) vars<-apply(x3,2,var) cbind(means,vars)> 3. I then want to repeat above procedure 500 times so that eventually I > will have 500 100x2 matrices.make.a.matrix<-function(...){ x3<-rpois(50*100, rep(mu,each=100)) x3<-ifelse(runif(50*100)<0.01, 0, x3) x3<-matrix(x3, ncol=50) cbind(apply(x3,2,mean), apply(x3,2, var)) } many.matrices<-lapply(1:500, make.a.matrix) gives a list of 500 matrices. This isn't quite the most efficient solution, but it's not bad. -thomas
The command ?apply will easily get row means/vars (or col means/vars) e.g. my.mat<-matrix(rnorm(144),12) my.mat apply(my.mat,1,mean,na.rm=T) apply(my.mat,1,var,na.rm=T) The functions ?rbind and ?cbind will help you add rows or columns to matrices or data frames Enjoy Wayne -----Original Message----- From: Haiyan Chen [mailto:hxc05@health.state.ny.us] Sent: 18 February 2004 15:39 To: R-help@stat.math.ethz.ch Subject: [R] How to repeat a procedure 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; 3. I then want to repeat above procedure 500 times so that eventually I will have 500 100x2 matrices. Would someone mind helping me to code 2 & 3? Thanks ahead of time. Heyen ______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html KSS Ltd Seventh Floor St James's Buildings 79 Oxford Street Manchester M1 6SS England Company Registration Number 2800886 Tel: +44 (0) 161 228 0040 Fax: +44 (0) 161 236 6305 mailto:kssg@kssg.com http://www.kssg.com The information in this Internet email is confidential and m...{{dropped}}
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? > >Thanks ahead of time. > >Heyen > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
Thanks, Patrick. I was aware of that and was able to fix it. Thanks Tom, Wayne and Peter for all your help. Heyen Patrick Burns <pburns at pburns.se To: Thomas Lumley <tlumley at u.washington.edu> anet.com> cc: Haiyan Chen <hxc05 at health.state.ny.us>, R-help at stat.math.ethz.ch Subject: Re: [R] How to repeat a procedure 02/18/2004 01:09 PM I believe that Thomas got "mu" wrong. If I understand correctly, the line: x3 <- rpois(50 * 100, rep(mu, each=100)) should read: x3 <- rpois(50 * 100, rep(mu, 50)) or just x3 <- rpois(50 * 100, mu) Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Thomas Lumley wrote:>On Wed, 18 Feb 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}, >> >> > >YOu can do this without the loop, eg > >x3<-rpois(50*100, rep(mu,each=100)) >x3<-ifelse(runif(50*100)<0.01, 0, x3) >x3<-matrix(x3, ncol=50) > > > >>2. I want to calculate means and sample variances of each row and createa>>new matrix 100x2; >> >> > >means<-apply(x3,2,mean) >vars<-apply(x3,2,var) >cbind(means,vars) > > > >>3. I then want to repeat above procedure 500 times so that eventually I >>will have 500 100x2 matrices. >> >> > >make.a.matrix<-function(...){ > x3<-ifelse(runif(50*100)<0.01, 0, x3) > x3<-matrix(x3, ncol=50) > cbind(apply(x3,2,mean), apply(x3,2, var)) >} > >many.matrices<-lapply(1:500, make.a.matrix) > >gives a list of 500 matrices. This isn't quite the most efficient >solution, but it's not bad. > > > -thomas > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html> > > >