[R] A function which is a sum of other functions...

David Winsemius dwinsemius at comcast.net
Thu Dec 19 20:24:38 CET 2013


On Dec 19, 2013, at 11:05 AM, Onur Uncu wrote:

> 
> Dear R Users
> 
> I have a list of functions. Each function in the list is a function of single variable. I would like to create a function (of one variable) which represents the sum of all the functions in the list. So, if the functions in my list are f1(x),..,f5(x) then I would like a new function f(x)=f1(x)+f2(x)+...f5(x)
> 
> Appreciate any suggestions on how to do this. 
> 
> I need the above f(x) function because I would like to minimise it with respect to x using the nlm function.

> fbig <- function(x, f1=I, f2=I, f3=I, f4=I, f5=I){
                              f1(x)+f2(x)+f3(x)+f4(x)+f5(x)}
> fbig(2)
[1] 10
> fbig(2, exp)
[1] 15.38906

> fbig(2, exp, log)
[1] 14.0822

Since "+" is vectorized this should not need to be wrapped in sapply as long as each function is itself vectorized.


David Winsemius
Alameda, CA, USA



More information about the R-help mailing list