Reputation: 53
I would like to define an arbitrary function of an arbitrary number of variables, for example, for 2 variables:
func2 <- function(time, temp) time + temp
I'd like to keep variable names that have a meaning in the problem (time and temperature above).
If I have a vector of values for these variables, for example in the 2-d case, c(10, 121), I'd like to apply my function (func2 here) and obtain the result. Conceptually, something like,
func2(c(10,121))
becomes
func2(10,121)
Is there a simple way to accomplish this, for an arbitrary number of variables?
Thanks
Upvotes: 0
Views: 139
Reputation: 3188
Is this what you are looking for?
f = function(f,...) {
v = list(...)
Reduce(f, v)
}
> f(f = "+", x = pi, y = exp(1), z = pi^2)
15.72948
Upvotes: 0
Reputation: 206167
You could write a helper function to turn a simple vector into parameters with the help of do.call
splat <- function(f,v) {
do.call(f, as.list(v))
}
splat(func2, c(10,121))
Upvotes: 1