Benjamin Telkamp Benjamin Telkamp - 26 days ago 13
R Question

Sampling Distribution from a data-set with one column

I want to create a sampling distribution for a mean. I have a variable

x
with at least ten thousand values. I want take 500 samples (n=10) and then show the distribution of the sample means in a histogram. I think it worked with the following, but can anyone check if this is what i meant and tell me what the
2
within the apply function stands for?

x <- rnorm(10000, 7.5, 1.5)
draws = sample(x, size = 10 * 500, replace = TRUE)
draws = matrix(draws, 10)
drawmeans = apply(draws, 2, mean)
hist(drawmeans)


would be sincerely appreciated!

Answer

You could do this using replicate if you wanted. One of lots of different ways. For data frame df

out = replicate(500, mean(sample(df$Scores,10)))
hist(out)