R exercise: for loops/sampling distribution

A good friend of mine has been learning how to use R for his research in community ecology for the last year. He reminded me today of an exercise I wrote up for him awhile back to introduce the use of for loops. The exercise is a demonstration of the sampling distribution and goes as such:

The sampling distribution describes the distribution of sample means. In this exercise a simple experiment is performed where a population is sampled, and the mean of that sample is recorded. This is to be repeated several times in a for loop, with each sample mean recorded so we may later examine their distribution.

  1. Create a population of size 10000, with some normally distributed trait (mean = 10, standard deviation = 10)
  2. Create a for loop that repeats 1000 times. Take a sample of size 100 from this population at every iteration, and save each sample mean.
  3. Create a histogram of your 1000 recorded sample means.
  4. Find the mean and standard deviation of your recorded sample means. Compare these to the mean and standard deviation of the original population.

Needed R functions: rnorm(), numeric(), for(){}, sample(), mean(), sd(), hist()


Leave a Reply