Saroupille - 1 year ago 182

R Question

I create two functions with R this way :

`esp<-function(x) x*dexp(x,lambda)`

then :

`esp2<-function(R) integrate(esp,0,R)`

I can plot the first one, but when I try to plot the second one I get an error :

`'expr' did not evaluate to an object of length n`

With some research, I have to use sapply function, or vectorize function. But first, I don't know why, and secondly it doesn't work.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You need to `Vectorize`

it because `curve`

expects such a function for it to calculate the plotting data. It doesn't work because `integrate`

outputs a complicated object. Therefore you need to isolate the relevant part of the output:

```
esp2 <- function(R) integrate(esp,0,R)$value
plot(Vectorize(esp2))
```