Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R error: cannot coerce type 'closure' to vector of type 'double'

Tags:

r

I'm getting an error in my R program that says:

Error in as.double(x) : cannot coerce type 'closure' to vector of type 'double'

Here's my code, I can't figure out where it's coming from:

norm.pop = rnorm(100000,10,sd = 1)
exp.pop = rexp(100000, rate = 1/10)
true.mean = 10

norm.func = function(n, N, type)
{
  if(type == "N")
    pop = norm.pop
  else if(type =="E")
    pop = exp.pop
  all.the.probs = sapply(1:N, function(i)
  {
    the.sample = sample(pop, size = n, replace = TRUE)
    x.bar = mean(the.sample)
    sd.norm = sd(sample)/sqrt(n)
    z.score = 1.96
    upper.fence = x.bar + z.score*sd.norm
    lower.fence = x.bar - z.score*sd.norm
    if((true.mean >= lower.fence) & (true.mean <= upper.fence))
    {
      return(1)
    }
    else
    {
      return (0)
    }
  })

  result = mean(all.the.probs)

  return (result) 
}

norm.func(10, 10000, "N")
like image 400
mattgabor Avatar asked Jun 04 '15 23:06

mattgabor


1 Answers

Change:

sd.norm = sd(sample)/sqrt(n)

to:

sd.norm = sd(the.sample)/sqrt(n)

You are trying to use the function sample (a closure) as a number (double)

like image 129
jeremycg Avatar answered Oct 01 '22 03:10

jeremycg