xBarGenerator code giving me an error in RStudio

I am getting an error while trying this. I also tried installing the qcc package and sued process.capability() but I am still getting an error. Can anyone help? Here is my code:

# Arguments: samplesize: the size of the sample that each sample mean is based on. 
# number_of_samples: the number of samples and thus sample means we will #generate

xbarGenerator = function(sampleSize = 30,number_of_samples = 100)
{
  for(i in 1:number_of_samples)
  {
    theSample = sample(population,sampleSize)
    xbar = mean(theSample)
    xBarVec = c(xBarVec, xbar)
  }
  return(xBarVec)
}

xbars = xbarGenerator(30,1000)
length(xbars)
hist(xbars)

The output I am getting is below:
> xbarGenerator = function(sampleSize = 30,number_of_samples = 100)
+ {
+   for(i in 1:number_of_samples)
+   {
+     theSample = sample(population,sampleSize)
+     xbar = mean(theSample)
+     xBarVec = c(xBarVec, xbar)
+   }
+   return(xBarVec)
+ }
xbars = xbarGenerator(30,1000)
Error in xbarGenerator(30, 1000) : object 'xBarVec' not found
> length(xbars)
Error: object 'xbars' not found
> hist(xbars)
Error in hist(xbars) : object 'xbars' not found
>

Can you be more specific? what is the error message you are getting? what is your expected output?
Also, please turn this into a proper REPRoducible EXample (reprex), right now we can't reproduce your code since you are not showing what population is or from where it comes from.

Hi, the population is coming from this code:
xBarVec = c() #Global vector to hold the sample means
population = rnorm(10000000,0,1) #Simulating the population

Well, you just have to add those lines to your function and it works

xbarGenerator = function(sampleSize = 30,number_of_samples = 100) {
    population = rnorm(10000000,0,1)
    xBarVec = c()
    for(i in 1:number_of_samples) {
        theSample = sample(population,sampleSize)
        xbar = mean(theSample)
        xBarVec = c(xBarVec, xbar)
    }
    return(xBarVec)
}

xbars = xbarGenerator(30,1000)
length(xbars)
#> [1] 1000
hist(xbars)

Created on 2020-01-07 by the reprex package (v0.3.0.9000)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.