R 的旋流编程练习“函数”中的错误

问题描述 投票:0回答:1

卡在 55% 完成度练习上。尝试使用定义如下的评估函数:

evaluate <- function(func, dat){
 func(dat)
}

坚持下一个练习:

Let's take your new evaluate() function for a spin! Use evaluate to find the standard deviation of the vector c(1.4,
| 3.6, 7.9, 8.8).

我的输入是

> evaluate(sd, c(1.4, 3.6, 7.9, 8.8))

我不断收到错误消息

Error in func(dat) : could not find function "func"

我尝试重新启动 swirl 并使用以下代码行,但无济于事。

> evaluate(func = sd, dat = c(1.4,3.6,7.9,8.8))

请帮忙。

r swirl
1个回答
0
投票

最可能的原因是

sd
函数已被覆盖:

evaluate <- function(func, dat){
  func(dat)
}

# Overwrite sd
sd <- NA

evaluate(sd, c(1.4, 3.6, 7.9, 8.8))
Error in func(dat) : could not find function "func"

rm(sd)

evaluate(sd, c(1.4, 3.6, 7.9, 8.8))
#> [1] 3.514138
© www.soinside.com 2019 - 2024. All rights reserved.