f <- function(x) {
# Does something
return # an integer
}
我该如何创建一个函数调用的向量,使其等同于以下表达式:
v(f(0), f(1), ..., f(n))
f <- function(x) {
# Does something
return # an integer
}
v(f(0), f(1), ..., f(n))
sapply
中循环遍历参数(因为如果输出元素是单个值,则sapply
返回一个vector
)。sapply(0:n, f)
或者我们将该函数进行向量化并传入多个值
Vectorize(f)(0:n)
sapply(0:n, f)
还是Vectorize(f)(0:n)
? - akrun