如何将多个参数作为单个向量传递给函数? [英] How can I pass multiple arguments to a function as a single vector?
问题描述
我用六个参数创建了以下函数:
I created the following function with six args:
nDone <- function(under,strike,ttoe,vol,rf,dy) {
pnorm(((log(under/strike)+ (rf-dy+(vol^2)/2)*ttoe)/(vol*(ttoe^0.5))))
}
nDone(90,100,3,0.17,0.05,0)
# Result:
[1] 0.6174643
现在,我在一个对象中创建一个具有相同值的向量,并尝试使用该向量调用该函数,但会出现以下错误:
Now I create a vector with the same values in an object, and try to call the function using the vector, but get the following error:
d <- c(90,100,3,0.17,0.05,0)
nDone(d)
Error in under/strike : 'strike' is missing
我做错了什么以及如何解决?
What am I doing wrong and how to fix?
推荐答案
尝试一下
do.call(nDone, as.list(d))
@joran 在评论中对您的第一次尝试的解释:
Explanation of what's happening in your first attempt by @joran from the comments:
R看到您向nDone
传递了一个参数,即向量d
,该参数已传递给第一个函数参数under
.由于您尚未为其他参数指定默认值,因此它们丢失了,因此会出现错误
R is seeing you pass a single argument to nDone
, namely the vector d
, which is handed off to the first function argument, under
. Since you haven't specified a default value for the others, they are missing and hence the error
这篇关于如何将多个参数作为单个向量传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!