简体   繁体   English

如何在R中定义函数的列表/向量?

[英]How do I define a list/vector of functions in R?

I have a function which takes another function as an input variable, eg. 我有一个函数,将另一个函数作为输入变量,例如。

WrapperFunction <- function(x, BaseFunction){
   y <- cor(x)
   BaseFunction(y)
}

Now I want to input various BaseFunctions into this WrapperFunction, to produce a vector of outputs, but how do I define a list of functions so that instead of plugging in each function by hand, I can automate the process with a for loop: 现在,我想在此WrapperFunction中输入各种BaseFunction,以产生输出向量,但是如何定义函数列表,这样我可以手动使用for循环使过程自动化,而不是手动插入每个函数:

for (i in 1:n){
   output[i] <- WrapperFunction(x, FunctionList[i])
}

I've tried defining 我尝试定义

FunctionList <- list()

FunctionList[1] = Function1 , etc....

, which didn't work. ,这没有用。

Nor did defining 定义也没有

FunctionList <- c("Function1", "Function2", ...)

I'm not sure what you're using for your x value, I presume it's a matrix. 我不确定您使用的x值是多少,我想它是一个矩阵。 You can put some functions into a list then use lapply . 您可以将一些函数放入列表中,然后使用lapply

For example 例如

m <- matrix(1:6, 2)
lst <- list(cov, diff)
lapply(lst, WrapperFunction, x = m)
#[[1]]
#              [,1]          [,2]          [,3]
#[1,]  1.848893e-32 -6.162976e-33 -6.162976e-33
#[2,] -6.162976e-33  1.848893e-32 -6.162976e-33
#[3,] -6.162976e-33 -6.162976e-33  1.848893e-32
#
#[[2]]
#              [,1]          [,2]         [,3]
#[1,] -2.220446e-16  2.220446e-16 0.000000e+00
#[2,]  0.000000e+00 -2.220446e-16 2.220446e-16

Also, as suggested by @nrussell, you can vectorize the BaseFunction argument, which is essentially the same as doing the above. 另外,如@nrussell所建议,您可以向量化BaseFunction参数,该参数与上述操作基本相同。

vWF <- Vectorize(WrapperFunction, vectorize.args = "BaseFunction")
vWF(m, lst)

This produces the same result as shown above. 这将产生与上述相同的结果。

If you want to have a list of functions, you can do something like: 如果您想获得功能列表,可以执行以下操作:

myFuns <- list(mean, sd)

And then you can lapply over this list, or use the for loop as you wanted. 然后,您可以将lapply在此列表上,或者根据需要使用for循环。 If you use the for loop make sure that you use the [[ syntax, because this makes sure that you are retrieving the function and not a length one list: 如果使用for循环,请确保使用[[语法,因为这确保您要检索的是函数而不是长度为1的列表:

for (i in 1:n){
    output[i] <- WrapperFunction(x, myFuns[[i]])
}

or 要么

lapply(myFuns, WrapperFunction, x = x)

A slightly different way, Hope this answers your question ... 稍微不同的方式,希望这能回答您的问题...

  ## Function 1
  fun1 <-function(a)
  {
    a = a+a
    return(a)
  }

  ## Function 2
  fun2 <- function(a)
  {
    a = a*a
    return(a)
  }

  ## Function 3
  fun3 <- function(a)
  {
  a = a^a
  return(a)
  }

  ## Vector of function names
  funlist <- c("fun1","fun2","fun3")

  ## Main Wrapper fnction
  mainfun <- function(x,funlist)
  {
    y = cor(x[,1],x[,2])

    res=NULL
    for(i in 1:length(funlist))
    {
      k <- eval(parse(text=paste(funlist[i],"(a=",y,")",sep="")),env=.GlobalEnv)
      res <- c(res,k)
    }

    return(c(y,res))
  }

  #############

  x=cbind(c(1:100),c(101:200))
  mainfun(x,funlist)

 [1] 1 2 1 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM