Take the 2-minute tour ×
 constant <- function(x){1}


this is probably more than ridiculously simple, but any reason as to why this isn't working?

is there a different way I should be writing the function for constants?

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

You can use the Vectorize function to convert a non-vectorized function into the sort of function that integrate requires:

> constant <- function(x){1}
> Vconst <- Vectorize(constant)
>  integrate(Vconst,lower=1,upper=10)
9 with absolute error < 1e-13
share|improve this answer
add comment

From ?integrate:

An R function taking a numeric first argument and returning a numeric vector of the same length. Returning a non-finite element will generate an error.

You need to return a vector the same length as x. Try:

constant <- function(x){rep(1,length(x))}
share|improve this answer
add comment

Your Answer


