【问题标题】:Create a list of functions from a vector of characters从字符向量创建函数列表
【发布时间】:2012-08-20 11:30:45
【问题描述】:

提前谢谢,如果这个问题之前已经回答过,我很抱歉 - 我已经看过很多了。我有一个数据集,其中包含一行连接信息,特别是:名称、颜色代码、一些函数表达式。例如,一个值可能是:

成本#FF0033@log(x)+6。

我拥有提取信息的所有代码,最后得到一个表达式向量,我想将其转换为实际函数列表。

例如:

func.list <- list()
test.func <- c("x","x+1","x+2","x+3","x+4")

其中 test.func 是表达式的向量。我想要的是:

func.list[[3]]

等价于

function(x){x+3}

我知道我可以使用以下方法创建函数:

somefunc <- function(x){eval(parse(text="x+1"))} 

将字符值转换为函数。当我尝试循环创建多个功能时,问题就来了。举一个我试过但没用的例子:

for(i in 1:length(test.func)){
  temp <- test.func[i]
  f <- assign(function(x){eval(expr=parse(text=temp))})
  func.list[[i]] <- f
}

基于另一篇文章 (http://stats.stackexchange.com/questions/3836/how-to-create-a-vector-of-functions) 我也试过这个:

makefunc <- function(y){y;function(x){y}}
for(i in 1:length(test.func)){
   func.list[[i]] <-  assign(x=paste("f",i,sep=""),value=makefunc(eval(parse(text=test.func[i]))))
 }

这给出了以下错误: eval(expr, envir, enclos) 中的错误:找不到对象“x”

最终目标是获取函数列表并将第 j 个函数应用于 data.frame 的第 j 列,以便脚本的用户可以指定如何在列给出的连接信息内对每一列进行规范化标题。

【问题讨论】:

    标签: r user-defined-functions


    【解决方案1】:

    也许用一个通用函数初始化您的列表,然后使用以下方法更新它们:

    foo <- function(x){x+3}
    > body(foo) <- quote(x+4)
    > foo
    function (x) 
    x + 4
    

    更具体地说,从一个角色开始,您可能会执行以下操作:

    body(foo) <- parse(text = "x+5")
    

    【讨论】:

    • 谢谢!!它工作得很好。一旦允许我这样做,我将添加用于完成任务的代码作为其他用户的附加答案。 (很抱歉所有的 cmets/edits/deletes/and whatnot - 我显然是 SO 新手)
    • @dayne 与普通的全新 SO 访问者相比,我想说你已经掌握了一切。
    【解决方案2】:

    只是为了补充 joran 的答案,这就是最终奏效的方法:

    test.data <- matrix(data=rep(1,25),5,5)
    test.data <- data.frame(test.data)
    
    test.func <- c("x","x+1","x+2","x+3","x+4")
    func.list <- list()
    
    for(i in 1:length(test.func)){
      func.list[[i]] <- function(x){}
      body(func.list[[i]]) <- parse(text=test.func[i])
    }
    
    processed <- mapply(do.call,func.list,lapply(test.data,list))
    

    再次感谢,乔兰。

    【讨论】:

      【解决方案3】:

      这就是我的工作:

      f <- list(identity="x",plus1 = "x+1", square= "x^2")
      funCreator <- function(snippet){
        txt <- snippet
        function(x){
          exprs <- parse(text = txt)
          eval(exprs)   
        }
      }
      listOfFunctions <- lapply(setNames(f,names(f)),function(x){funCreator(x)}) # I like to have some control of the names of the functions
      listOfFunctions[[1]] # try to see what the actual function looks like?
      library(pryr)
      unenclose(listOfFunctions[[3]]) # good way to see the actual function http://adv-r.had.co.nz/Functional-programming.html
      # Call your funcions
      listOfFunctions[[2]](3) # 3+1 = 4
      do.call(listOfFunctions[[3]],list(3)) # 3^2 = 9
      attach(listOfFunctions) # you can also attach your list of functions and call them by name
      square(3)  # 3^2 = 9
      identity(7) # 7 ## masked object identity, better detach it now!
      detach(listOfFunctions)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-25
        • 2021-06-25
        • 2013-10-04
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多