Re: [R] R object as a function

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue, 22 Jan 2008 06:52:48 -0500

On 22/01/2008 5:30 AM, Thomas Steiner wrote:
> I want to use a function as an argument to ingtegrate it twice.
> See the following (senseless) example of a double integration:
>
> test<-function(sf,lo,up,rest) {
> innerFkn<-function(sf,lo) {
> inte=integrate(f=sf,lower=lo,upper=4)
> return( inte$value )
> }
> integral=integrate(f=innerFkn,lower=1,upper=2,sf=sf,lo=lo,up=up)
> return( integral$vlaue+rest )
> }
> test(sf=stepfun(c(0,1),c(2,-1,3)),lo=0,up=2,rest=12)
>
> Why isn't it possible to define the "innerFkn" inside "test"?

It is possible, but it needs to take arguments in the order that integrate expects, i.e. the first argument needs to be the values at which it will be evaluated. You don't specify any "x" value.

The other problem is that integrate is not vectorized, it can only take scalar values for lower and upper, so you'll need a loop within innerFkn to do the integral over the values being passed in.

Duncan Murdoch

> "sf" is a stepfun, but it should possibly be any function.
> How can I define some R object like a stepfun (depending on variables)
> which can be evaluated like a function at some "lo"?
> Thanks for help,
> Thomas
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> 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.



R-help_at_r-project.org mailing list
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. Received on Tue 22 Jan 2008 - 11:56:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Jan 2008 - 16:30:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive