R 如何将多个参数作为单个向量传递给函数?

R 如何将多个参数作为单个向量传递给函数?,r,function,arguments,parameter-passing,R,Function,Arguments,Parameter Passing,我用六个参数创建了以下函数: 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 nDone试试这个 do.call(nDone, as.list(d)) 从评论中解释您第一次尝试

我用六个参数创建了以下函数:

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
nDone试试这个

 do.call(nDone, as.list(d))

从评论中解释您第一次尝试时发生的情况:

R是指将单个参数传递给
nDone
,即向量
d
,它被传递给
下的第一个函数参数
。由于您尚未为其他项指定默认值,因此缺少这些项,因此可能需要添加错误:

如果函数可以接受长度大于1的向量参数并生成相同长度的输出,
do.call
也可以处理该问题,您需要
list()

或者一条整齐的单行线:

> seq(1,6) %>% exp() %>% scales::rescale() %>% list() %>% rep(3) %>% do.call(rgb,.)
[1] "#000000" "#030303" "#0B0B0B" "#212121" "#5D5D5D" "#FFFFFF"

我就在这里,打了一个长长的解释!;)我要添加到这段代码中的部分内容是:R看到你将一个参数传递给
nDone
,即向量
d
,它被传递给第一个函数参数
。由于您尚未为其他答案指定默认值,因此它们将丢失,因此会出现错误。抱歉–我猜我的回答风格已被R-help彻底破坏。谢谢,是的,这是一个更好的标题。
x <- c("a", "b", "c")
y <- c(1, 2, 3)

> do.call(paste0,c(list(x),list(y)))
[1] "a1" "b2" "c3"
x <- c("a", "b")

> do.call(paste0,c(list(x),list(y)))
[1] "a1" "b2" "a3"
# whichever complex functions to generate vector of floats:
x <- seq(1,6) %>% exp()

# rescale for rgb
x <- scales::rescale(x)

# make a list of vectors
# note that as.list() would not give the desired output here
x <- rep(list(x),3)

# call
> do.call(rgb, x)
[1] "#000000" "#030303" "#0B0B0B" "#212121" "#5D5D5D" "#FFFFFF"
> seq(1,6) %>% exp() %>% scales::rescale() %>% list() %>% rep(3) %>% do.call(rgb,.)
[1] "#000000" "#030303" "#0B0B0B" "#212121" "#5D5D5D" "#FFFFFF"