Hi I try to create a loop for uniform Distribution for my homework
and this is my code, when I run i get this error, "Error in do.call(c, mean_uni) :
'what' must be a function or character string
In addition: There were 50 or more warnings (use warnings() to see the first 50)"
can someone help me fix this
Uniform_Distribution = function(samples, size, min, max){
uni_size = samples
set.seed(22)
uni = list()
for (i in 1:uni_size) {
uniform_samples = runif(n = size, min, max)
uni[[i]] = uniform_samples
}
mean_uni = list()
for (j in 1:length(uni)) {
mean1 = mean(uni[j])
mean_uni[[j]] = mean1
}
If you want to access an element of a list, use [[ instead of [. Actually, you could have used sapply to get as a vector instead of list directly in that way.
If I understand what you are trying to do, this is probably a better way:
x <- replicate(100, runif(30, 0, 1))
y <- colMeans(x)
Hope this helps.
Edit:
Yes, none of the for loops are required any more. replicate will handle the first one, and colMeans the 2nd one.