[R] How to use curve() function without using x as the variable name inside expression?

Rolf Turner r.turner at auckland.ac.nz
Sat Jan 31 09:39:10 CET 2015


On 31/01/15 21:10, C W wrote:
> Hi Bill,
>
> One quick question.  What if I wanted to use curve() for a uniform
> distribution?
>
> Say, unif(0.5, 1.3), 0 elsewhere.
>
> My R code:
> func <- function(min, max){
>    1 / (max - min)
> }
>
> curve(func(min = 0.5, max = 1.3), from = 0, to = 2)
>
> curve() wants an expression, but I have a constant.   And I want zero
> everywhere else.

Well if that's what you want, then say so!!!

func <- function(x,min,max) {
    ifelse(x < min | x > max, 0, 1/(max - min))
}

curve(func(u,0.5,1.3),0,2,xname="u")

Or, better (?) curve(func(u,0.5,1.3),0,2,xname="u",type="s")

which avoids the slight slope in the "vertical" lines.

cheers,

Rolf Turner

-- 
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619



More information about the R-help mailing list