如何将多个参数作为单个向量传递给函数? [英] 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屋!