[R] Problems with sample variance

Daniel Nordlund djnordlund at verizon.net
Thu May 21 07:22:35 CEST 2009

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Alspach
> Sent: Wednesday, May 20, 2009 10:02 PM
> To: milton ruser; Chloe Smith
> Cc: r-help at r-project.org
> Subject: Re: [R] Problems with sample variance
> Thanks Milton - I misread Chloe's original question.
> nSims <- 1000
> sampSiz <- 15
> myVars <- tapply(rnorm(nSims*sampSiz), rep(1:nSims, sampSiz), var)
> This is a little more succinct, although I do not know enough about
> random number generation to be certain about the effect of breaking a
> single call to rnorm into 1000 bits, rather than having 1000 calls to
> rnorm (or even if there is such an effect.  Comment anyone?).

It doesn't matter whether you use a single call or multiple calls, the same
set of numbers will be generated either way (if one starts with the same

> It used to be (and I imagine still is) not efficient 
> incrementally grow
> an object in a loop [as is done in your code with variance.list <-
> c(variance.list, var(mysample))].  Better to create 
> variance.list at its
> full size first (filled with NA) and then populate it.  Not that it is
> likely to make too much difference with these numbers.
> HTH .....
> Peter Alspach

Hope this is helpful,


Daniel Nordlund
Bothell, WA USA

More information about the R-help mailing list