0
votes

I want to do some things :

  • Draw 100 times 50 number's from normal distribution with

    mean = 10 and standard deviation = 20

  • For any draw i want to count his standard deviation and arithmetic mean.

  • At the end i want to create a vector which has a length 100, containing the absolute value of the difference of the standard deviation and the arithmetic mean. (i.e i want to create some vector x that x[i]=|a-b|, where a is the standard deviation of 100 numbers in i-th draw, and b is the mean of 100 number's in i-th draw.

What i Did :

Creating 100 draw's from normal distribution above :

replicate(100, rnorm(50, 10, 20), simplify = FALSE)

Now i have a problem. I know that i can use functions "mean" and "sd" to count arithmetic mean and standard deviation, but i have to define number's that i draw as a vector. What i mean :

Number's that i rolled in first draw - vector 1

Number's that i rolled in second draw - vector 2

And so on

Then i can count their arithmetic mean and standard deviation.

Then we can count |a-b| (define above). And at the end i will create the vector that x[i]=|a-b|.

I have an idea but i don't know how to write it.

1

1 Answers

2
votes

This is a matter of assigning the result of replicate to a variable (of class "list", since simplify = FALSE) and then sapply the mean and sd functions.

set.seed(1234)    # Make the results reproducible

repl <- replicate(100, rnorm(50, 10, 20), simplify = FALSE) 

mu <- sapply(repl, mean)
s <- sapply(repl, sd)
D <- abs(s - mu)

head(D)
#[1] 16.761930  7.953432  6.833691 12.491605  5.490149  6.850794

A one-liner could be

D2 <- sapply(repl, function(x) abs(sd(x) - mean(x)))
identical(D, D2)
#[1] TRUE