Indexed function

Everything, I'm wondering how to index the function f (x)

f1<-function(x) x/2+5
f2<-function(x) x/3-10

      

So, I can call the function something like

f[1](x)

      

Thank!

+3


source to share


1 answer


Keep related functions in a list, don't rely on magic names.



f <- list(
    function(x) x/2+5,
    function(x) x/3-10
)

f[[1]](5)
# [1] 7.5

f[[2]](5)
# [1] -8.333333

      

+4


source







All Articles