【发布时间】:2018-03-17 23:21:00
【问题描述】:
bmi<-function(x,y){
(x)/((y/100)^2)
}
bmi(70,177) 可以工作
但是使用 apply() 不起作用
apply(Student,1:2,bmi(Student$weight,Student$height))
match.fun(FUN) 中的错误: 'bmi(Student$weight, Student$height)' 不是函数、字符或符号
【问题讨论】:
-
你的函数已经被向量化了,所以你不需要
apply,只需要bmi(Student$weight,Student$height) -
补充@docendodiscimus 刚才所说的内容。
Student$bmi <- bmi(Student$weight, Student$height)如果你想要一个带有 bmi 的列。 -
apply(Student, 1, function(x) bmi(x["weight"], x["height"])),但不推荐。