R错误:无法强制将类型'闭包'强制转换为'double'类型的向量

mat*_*bor 2 r

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

as.double(x)中的错误:无法将类型'closure'强制类型为'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")
Run Code Online (Sandbox Code Playgroud)

jer*_*ycg 8

更改:

sd.norm = sd(sample)/sqrt(n)
Run Code Online (Sandbox Code Playgroud)

至:

sd.norm = sd(the.sample)/sqrt(n)
Run Code Online (Sandbox Code Playgroud)

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