Saroupille - 8 months ago 90
R Question

# R : Error expr did not evaluate to an object of length n

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.

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