我正试图将一个函数应用于一系列输入,但我遇到了这个错误。我不明白为什么,因为我相信我在提供的数据帧中传递了"cla"的值。如果这是一个太基本的问题,我很抱歉,但我想不通。下面是一个可重复的例子。
我需要的输出是"inputs"数据帧的每一行的responseRate。
提前谢谢。
inputs <- data.frame(P=c(0, 50, 80, 100),
pd=c(38, 50, 50, 86),
cla=c(15, 40, 30, 81))
eq1 <- function(a,b,c,x){
(a*exp(-((((exp(b)*x))/1000))+c+b))/1000
}
resp.function <- function(pd, cla, eq1, P){
a <- 70 + pd*80 + cla*80
b <- 1
c <- 70 + pd*10
responseRate <- eq1(a,b,c,P)
return(responseRate)
}
apply(inputs, 1, resp.function)
您不需要在resp.function
中传递eq1
。
resp.function <- function(pd, cla, P){
a <- 70 + pd*80 + cla*80
b <- 1
c <- 70 + pd*10
responseRate <- eq1(a,b,c,P)
return(responseRate)
}
此外,你的函数是矢量化的,所以你可以做:
resp.function(inputs$P, inputs$pd, inputs$cla)
如果函数未矢量化,则可以使用mapply
将其应用于行中的每个值。
mapply(resp.function, inputs$P, inputs$pd, inputs$cla)