In R what is the equivalent of @ function handle in Matlab?

后端 未结 2 1505
野性不改
野性不改 2021-01-18 22:58

In Matlab if I have a function f such as the signature is f(a,b,c), I can make a function which has only one variable b, which would call f with a fixed a=a1 and c=c1:

相关标签:
2条回答
  • 2021-01-18 23:30
    g <- function(b) f(a1, b, c1)
    
    0 讨论(0)
  • 2021-01-18 23:33

    There is also the convenient functional::Curry functional:

    f <- function(a, b, c) {a + b + c}
    f(1, 2, 3)
    # [1] 6
    
    library(functional)
    g <- Curry(f, a = a1, c = c1)
    g(b=2)
    # [1] 6
    g(2)
    # [1] 6
    

    I think an important difference with @NPE's solution is that the definition of g using Curry does not mention b. So you might prefer this approach when the number of arguments in f becomes large.

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