-1

What can I do to compel R to give me a sample less than 20, such that the sample will have mean = 0 and variance = var.

MWE

rnorm(20, mean=0, sd=1)

As the sample size gets larger, the sample gets closer to normal. How then can I make R to give me a sample of n < 20 with a mean equal to zero and variance equal to whatever I specify?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Daniel James
  • 1,381
  • 1
  • 10
  • 28

1 Answers1

4

Try

rnorm(20, mean=0, sd=sqrt(var))

For what it's worth

  • your last paragraph sounds like a slightly confused statement of the Central Limit Theorem; there's no difficulty in creating small data sets that are normal, or Gaussian (the CLT says that sums of N independent, identically distributed variables approach normality as N goes to infinity ...)
  • it would be worth using v rather than var to denote your variance, since var() is a built-in function in R (this is mostly harmless, but occasionally confusing).
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453