[R] Apply a multi-variable function to a vector
William Dunlap
wdunlap at tibco.com
Fri Sep 9 23:16:58 CEST 2016
Try do.call(), as in
> func2 <- function(time, temp) paste(time, temp)
> func2(121, 10)
[1] "121 10"
> do.call(func2, as.list(c(121,10)))
[1] "121 10"
> do.call(func2, list(121,10))
[1] "121 10"
>
> func2(121, time=10:12)
[1] "10 121" "11 121" "12 121"
> do.call(func2, list(121,time=10:12))
[1] "10 121" "11 121" "12 121"
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Sep 9, 2016 at 12:50 PM, Stephen Kennedy <stevek9123 at gmail.com>
wrote:
> Hello,
>
>
>
> 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 (in this case func2) 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? I’d like something that would simply work from the definition
> of the function. If that is possible.
>
>
>
> Thanks,
>
>
>
> Steve Kennedy
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help
mailing list