Search code examples
rfunctioncurve

How to determine the last "x" used in curve() in R


Although, one must always themselves determine the range of x when using curve() in R, I was wondering how I could get the last x used (i.e., to =) after a curve() is executed in R?

For example, if I save a curve() as an object called cc, I can get the first x (i.e., from =) from the curve() using: cc$x[1] (see below). But how can I get the last x used in this curve()?

As an example ]

cc = curve(dchisq(6, df = 3, ncp = x ), from = 0, to = 10, col = 'red')

First.x.used.in.curve = cc$x[1]

Last.x.used.in.curve = ?     ## How can I find this?

Solution

  • Just use tail to get the last n elements of the vector cc$x

    tail(x = cc$x, n = 1)
    #[1] 10
    

    Other possible ways would be

    rev(cc$x)[1] #Reverse and access the first element of the reversed vector
    #[1] 10
    
    #OR
    
    cc$x[length(cc$x)] #Index the last element by using the length of the vector
    #[1] 10