# Re: [R] integration problem

From: Petr Pikal <petr.pikal_at_precheza.cz>
Date: Wed 27 Oct 2004 - 17:47:47 EST

Hi Christoph

I use this function to calculate an area under curve. It integrates along the whole wavelength set or you can restrict the calculation by setting dm and hm to some appropriate values.

It also gives you two values. The first is total area to the x axis (y=0) and the second is only area below the curve and above line from min(x) to max(x).

integ1<-function (x,y,dm=-Inf,hm=+Inf)
{
if(dm==-Inf)dm<-min(x)
if(hm==+Inf)hm<-max(x)
vyber<-x<=hm&x>=dm

```l<-length(x[vyber])
v<-diff(x[vyber])
z<-y[vyber][1:l-1]+y[vyber][2:l]
o<-z*v/2
```

osum<-sum(o)
o1<-
(y[x==min(x[vyber])]+y[x==max(x[vyber])])*(max(x[vyber])- min(x[vyber]))/2
cista<-osum-o1
return(c(osum,cista))
}

I have also a function for specifying region for integration from plotted picture, if you are interested.

Cheers
Petr

On 26 Oct 2004 at 17:54, Christoph Scherber wrote:

> Dear R users,
>
> I have spectral data (say, wavelength vs. extinction coefficient) for
> which IŽd like to calculate an integral (i.e. the area underneath the
> curve).
>
> Suppose the (artificial) dataset is
>
> lambda E
> 1 2
> 2 4
> 3 5
> 4 8
> 5 1
> 6 5
> 7 4
> 8 9
> 9 8
> 10 2
>
>
>
> How can I calculate an integral for these values using R?
>
> Many thanks for any help!
> Regards
>
> Christoph
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help