I simulate with replicate()
the distribution of the mean of ten numbers by repeating the operation five times.
> set.seed(123)
> z <- replicate(5, mean(rnorm(10))
I would like to create with sapply
a second vector q
such that
> identical(z, q)
[1] TRUE
Despite of the rich documentation in R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate and in the help
files, I fail to produce a code with sapply
capable of integrate the five-times repetition that produced z
.
Indeed, the best result I got includes replicates()
itself:
set.seed(123)
q <- sapply(replicate(5, rnorm(10)), mean)
and it produces a vector q
of length 50, instead of a vector of length 5 as in z
. I am afraid it is an egg of Columbus that I am looking from the wrong perspective.