Search code examples
rdataframeapproximation

Specifying x values when converting approx() to data frame


I am trying to get a data frame from the output of approx(t,y, n=120) below. My intent is for the input values returned to be in increments of 0.25; for instance, 0, 0.25, 0.5, 0.75, ... so I've set n = 120.

However, the data frame I get doesn't return those input values.

t <- c(0, 0.5, 2, 5, 10, 30)
z <- c(1, 0.9869, .9478, 0.8668, .7438, .3945)
data.frame(approx(t, z, n = 120))

I appreciate any assistance in this matter.


Solution

  • There are 121, not 120, points from 0 to 30 inclusive in steps of 0.25

    length(seq(0, 30, 0.25))
    ## [1] 121
    

    so use this:

    approx(t, z, n = 121)
    

    Another approach is:

    approx(t, z, xout = seq(min(t), max(t), 0.25))