In R, what is equivalent to the @ function descriptor in Matlab?

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

g = @(b) f(a1, b, c1);

Is there an equivalent in R, or do I just need to override a new function?

+5
source share
2 answers

There is also a convenient functional functional::Curry:

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 the important difference with @NPE's solution is that the definition gusing Currydoes not mention b. Thus, you may prefer this approach when the number of arguments in fbecomes large.

+7
source
g <- function(b) f(a1, b, c1)
+5
source

All Articles