R错误:不能强制类型'关闭'到'' double'类型的向量

时间:2015-06-04 23:36:40

标签: r

我的R程序中出现错误,说:

  

as.double(x)出错:无法强制键入'关闭'矢量的   键入' double'

这是我的代码,我无法弄清楚它的来源:

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")

1 个答案:

答案 0 :(得分:8)

变化:

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

为:

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

您正尝试将函数sample(闭包)用作数字(双精度)