这是我的代码和输出:
by(ncbirths$weight, ncbirths$habit, mean)
Error in FUN(X[[1L]], ...) : could not find function "FUN"
如果我尝试将其他常用函数(如sd和summary)传递给by(),我会得到预期的输出。
答案 0 :(得分:0)
您最有可能将某些内容分配给名为mean
的变量,该变量屏蔽base::mean()
函数。您可以通过查看conflicts()
列表来确认这一点。例如
mean <- "doesnotexist"
ncbirths <- data.frame(weight=rnorm(20, 100,20), habit=rep(1:2, each=10))
by(ncbirths$weight, ncbirths$habit, mean)
# Error in FUN(X[[1L]], ...) : could not find function "FUN"
tapply(ncbirths$weight, ncbirths$habit, mean)
# Error in get(as.character(FUN), mode = "function", envir = envir) :
# object 'doesnotexist' of mode 'function' was not found
但是,如果为均值设置的值是函数的名称,tapply()
仍然有效,因为它使用&#34; match.fun&#34;解析FUN=
参数而by()
没有,并尝试直接调用该函数
mean <- "mean"
by(ncbirths$weight, ncbirths$habit, mean)
# Error in FUN(X[[1L]], ...) : could not find function "FUN"
tapply(ncbirths$weight, ncbirths$habit, mean)
# 1 2
# 108.16228 96.78932
请务必删除本地版本的平均值
rm(mean)
或明确调用基础版本
by(ncbirths$weight, ncbirths$habit, base::mean)