How to create a vector of functions?

后端 未结 1 937
北荒
北荒 2020-12-09 17:28

I would like to create a vector of functions using a two agruments function \'func\', for instance this one:

func = function(number, coefficient) {  
     re         


        
1条回答
  •  囚心锁ツ
    2020-12-09 17:51

    Use a factory function with a closure over its argument (which will hold the value of the looping variable):

    > # the factory function
    > makefunc <- function(x) { x; function() x }
    > funclist <- list()
    > for (i in 1:3) funclist[[i]] <- makefunc(i)
    > funclist[[1]]()
    [1] 1
    > funclist[[2]]()
    [1] 2
    > 
    

    0 讨论(0)
提交回复
热议问题