0

I have a function that perfectly works, and I call it tempQ which is a function of X. I want to create a new function in which every value less than zero is zero. So I write:

Qpdf <- function(X) {
                           if ( tempQ(X) > 0 ) tempanswer <- tempQ(X)  
                           else tempanswer <- 0   
                           return(tempanswer)

                         }  

This new Qpdf(X) works pretty well when I give some test numbers, but when I want to plot it, R gives me this error message:

> plot.function(Qpdf, from = 200 , to= 600)

Error in curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab,  : 
  'expr' did not evaluate to an object of length 'n'
In addition: Warning message:
In if (tempQ(X) > 0) tempanswer <- tempQ(X) else tempanswer <- 0 :
  the condition has length > 1 and only the first element will be used  

Actually, I want this function because I want to integrate it and I do not need its negative values to be in the integral intervals, but it does not for the integral to and gives me the same error:

> integrate(Qpdf , 200 , 800)

Error in integrate(Qpdf, 200, 800) : 
  evaluation of function gave a result of wrong length
In addition: Warning message:
In if (tempQ(X) > 0) tempanswer <- tempQ(X) else tempanswer <- 0 :
  the condition has length > 1 and only the first element will be used

How can I solve this problem?

Novic
  • 351
  • 1
  • 11

0 Answers0