在R函数中管理参数输入值的使用

在R函数中管理参数输入值的使用,r,function,R,Function,我的函数foo是用来计算一个名为d的变量,使用它的一个参数:s、t或f 例如,如果用户使用foo(n=30:35,s=1:2,t=3:5,f=7)我希望foo首先使用t计算d,然后f,然后s。因此,在输出中,我期望6ds(即2来自t,1来自f,2来自s) 我希望前两个ns代表t,下一个n代表f,剩下的ns代表s。有没有一种方法可以这样管理函数中的ns foo <- function(n, s = NULL, t = NULL, f = NULL){ d1 <- if(!is.nul

我的函数
foo
是用来计算一个名为
d
的变量,使用它的一个参数:
s
t
f

例如,如果用户使用
foo(n=30:35,s=1:2,t=3:5,f=7)
我希望
foo
首先使用
t
计算
d
,然后
f
,然后
s
。因此,在输出中,我期望6
d
s(即2来自
t
,1来自
f
,2来自
s

我希望前两个
n
s代表
t
,下一个
n
代表
f
,剩下的
n
s代表
s
。有没有一种方法可以这样管理函数中的
n
s

foo <- function(n, s = NULL, t = NULL, f = NULL){

d1 <- if(!is.null(t)) t + n else NULL
d2 <- if(!is.null(f)) f^2 + n else NULL

m <- if(!is.null(s)) s/2 + n else NULL
r <- if(!is.null(m)) m + 1 else NULL

d3 <- if(!is.null(m)) m + r else NULL

data.frame(d = c(d1, d2, d3))
}

 # Example of use:
 foo(n = 30:35, s = 1:2, t = 3:5, f = 7)

foo我们可以循环遍历't','f','s'的元素,用'n'添加(
++
),并在
列表中获得输出

foo <- function(n, s = NULL, t = NULL, f = NULL){

      dts <-  if(!is.null(t)) sapply(t, `+`, n) else NULL
      dfs <- if(!is.null(f)) sapply(f^2, `+`,  n) else NULL

      ms <- if(!is.null(s)) sapply(s/2, `+`,  n) else NULL
      rs <- if(!is.null(ms)) ms + 1 else NULL

      dss <- if(!is.null(ms)) ms + rs else NULL

      list(t_out =dts, f_out = dfs, s_out = dss)
      }

 # Example of use:
foo(n = 30:35, s = 1:2, t = 3:5, f = 7)

foo谢谢,我真正的函数不会使用
+
我只是用了一个简单的例子。但我想我无法实现我的想法。无论如何,非常感谢。@r如果这是一个复杂的函数,请使用匿名函数调用
sapply(t,function(x)yourfunction(x,n))
Arun,很难实现吗?