[R] Linear Interpolation question

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 30 01:37:18 CEST 2010


On Thu, Jul 29, 2010 at 5:16 PM, Ralf B <ralf.bierig at gmail.com> wrote:
> Hi R experts,
>
> I have the following timeseries data:
>
> #example data structure
> a <- c(NA,1,NA,5,NA,NA,NA,10,NA,NA)
> c <- c(1:10)
> df <- data.frame(timestamp=a, sequence=c)
> print(df)
>
> where i would like to linearly interpolate between the points 1,5, and
> 10 in 'timestamp'. Original timestamps should not be modified. Here
> the code I use to run the interpolation (so far):
>
> # linear interpolation
> print(c)
> results <- approx(df$sequence, df$timestamp, n=NROW(df))
> print(results)
> df$timestamp <- results$y
>
> # plotting
> plot(c, a, main = "Linear Interpolation with approx")
> points(results, col = 2, pch = "*")
>
> # new dataframe
> print(df)
>
> when looking at the result dataframe however, I can see that the
> original timestamps have been shifted as well. however would i would
> like to have is a result where the timestamps at position 2,4 and 8
> remain unchanged at the values 1,5, and 10. I also would like values
> before the first item to be constant. So the dataframe should look
> like this:
>
>
>  timestamp sequence
> 1   1.000000        1
> 2  1.000000        2
> 3   3.000000        3
> 4   5.000000        4
> 5   6.250000        5
> 6   7.500000        6
> 7   8.750000        7
> 8   10.00000        8
> 9   10.000000      9
> 10 10.000000     10
>
> How do I have the change the syntax of my script to make that work?
>

Try this:

library(zoo)

# test data
z <- zoo(c(NA,1,NA,5,NA,NA,NA,10,NA,NA), 1:10)

# linear approx
z.na <- na.approx(z, rule = 2); z.na

# plot both series
plot(cbind(z.na, z), screen = 1,
   type = c("o", "p"), pch = c("+", "o"), col = 1:2)



More information about the R-help mailing list