Dear R users, I am a beginner to R. I generated 1000 samples with 15 data in each sample I tried finding the variance for each sample I used the code: m=1000;n=15> r<-rnorm(15000) > for(i in 1:m){x=data[,i] v=var(x)} what I got was just the variance for the last sample i.e. the 1000th sample but what I want is 1000 variance. Does anyone know what I did wrong? Thanks Chloe Smith -- View this message in context: http://www.nabble.com/Problems-with-sample-variance-tp23645919p23645919.html Sent from the R help mailing list archive at Nabble.com.
Chloe Smith wrote:> > > [snip] > > what I got was just the variance for the last sample i.e. the 1000th > sample > > but what I want is 1000 variance. > >m=1000;n=15 r<-matrix(rnorm(m*n),nrow=m) apply(r,1,var) -- View this message in context: http://www.nabble.com/Problems-with-sample-variance-tp23645919p23646590.html Sent from the R help mailing list archive at Nabble.com.
Tena koe Chloe Try var(r) BTW, it is better to use more than single letters in name your objects to reduce the chance of conflicting with a standard R object. HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Chloe Smith > Sent: Thursday, 21 May 2009 12:33 p.m. > To: r-help at r-project.org > Subject: [R] Problems with sample variance > > > Dear R users, > > I am a beginner to R. I generated 1000 samples with 15 data > in each sample > > I tried finding the variance for each sample > > I used the code: > > m=1000;n=15 > > r<-rnorm(15000) > > for(i in 1:m){ > x=data[,i] > v=var(x)} > > what I got was just the variance for the last sample i.e. the > 1000th sample > > but what I want is 1000 variance. > > Does anyone know what I did wrong? > > Thanks > > Chloe Smith > > -- > View this message in context: > http://www.nabble.com/Problems-with-sample-variance-tp23645919 > p23645919.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
Hi Chloe, Try this: NUMBER.OF.RUNS<-10 #change here for (NR in 1:NUMBER.OF.RUNS) { number.simulation<-1000 sample.size=15; variance.list<-NULL for(i in 1:number.simulation) { mysample<-rnorm(sample.size) variance.list<- c(variance.list, var(mysample)) } #i hist(variance.list) } #NR Bests milton brazil=toronto On Wed, May 20, 2009 at 8:16 PM, Chloe Smith <hapiasfion@hotmail.com> wrote:> > Dear R users, > > I am a beginner to R. I generated 1000 samples with 15 data in each sample > > I tried finding the variance for each sample > > I used the code: > > m=1000;n=15 > > r<-rnorm(15000) > > for(i in 1:m){ > x=data[,i] > v=var(x)} > > what I got was just the variance for the last sample i.e. the 1000th sample > > but what I want is 1000 variance. > > Does anyone know what I did wrong? > > Thanks > > Chloe Smith > > -- > View this message in context: > http://www.nabble.com/Problems-with-sample-variance-tp23645919p23645919.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Apparently Analagous Threads
- My very first loop!! I failed. May I have some start-up aid?
- Sampling problems
- Which Durbin-Watson is correct? (weights involved) - using durbinWatsonTest and dwtest (packages car and lmtest)
- rsync
- My very first loop!! I failed. May I have some start-up aid?