How to make a linear interpolation for different dates using the function "approx"?

I have a data frame with three measurements throughout the year and I would like to make an interpolation between these dates to get values every two months. I used the approx function, but I don't know how to include the values in the correct position of the data.frame. Can anyone help me?

I used the script below:

approx(fev17[bloco=1&trat==1],set17[bloco=1&trat==1], method="linear", rule=1, f=0, ties="ordered", yleft = 0, yright = 0, n=32)

I don't understand the argument "xout" of the function "approx".

Thanks!

Upvotes: 2

Views: 2074

Answers (1)

otwtm
otwtm

Reputation: 1999

The argument xout specifies the grid of your x-axis on which you want to do the interpolation.

Here is an example how you can do it. Let's say you have the following data every 4 months:

   date <-seq(as.Date("2016/1/1"), as.Date("2019/1/1"), "4 months")
    values <- (rnorm(length(date)))
    df <- data.frame(date, values)
    x <- df$date
    y <- df$values
    plot(x,y, type='b')

enter image description here

Then you can use the function approx, where you define xout as is the monthly time axis of the same time period as x:

df <- approx(x=x,y=y, xout=seq(as.Date("2016/1/1"), as.Date("2019/1/1"), "months"))
plot(df$x, df$y, type="b")

enter image description here

Upvotes: 3

Related Questions