Saroupille Saroupille - 1 year ago 182
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.

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download