Makes sense. Simple and works. Thank you Greg.
Sent from my iPhone
> On 20 Dec 2013, at 16:15, Greg Snow <538...@gmail.com> wrote:
>
> My first thought was to use Reduce, but I think for this case that is
> a bit of overkill. You can have a vector or list of functions and
> just use sapply/lapp
My first thought was to use Reduce, but I think for this case that is
a bit of overkill. You can have a vector or list of functions and
just use sapply/lapply on the list of functions then sum the result.
A quick example:
> funs <- c(sin,cos,tan)
> sapply( funs, function(f) f(pi/6) )
[1] 0.50
Onur Uncu [mailto:onuru...@gmail.com]
> Sent: Thursday, December 19, 2013 11:43 AM
> To: William Dunlap
> Subject: Re: [R] A function which is a sum of other functions...
>
> Thanks William. May I please ask why we needed to use lapply inside Reduce?
> It sounds
> like we took a
> List <- list(abs, function(x)x*10, function(x)x*100)
> f <- function(x)Reduce(`+`, lapply(List, function(func)func(x)))
> f(-1:2)
[1] -1090 111 222
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun
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 i
5 matches
Mail list logo