0

I want to adjust the end values of interpolation for missing values in such a way that the difference between the next set of non-NA values are applied to determine the first one. for example, this is the code

library(zoo)
> na.approx(c(NA,10,8,NA,7,NA,NA,NA,11),na.rm = FALSE)
[1]   NA 10.0  8.0  7.5  7.0  8.0  9.0 10.0 11.0
> na.approx(c(NA,10,8,NA,7,NA,NA,NA,11), na.rm = FALSE, rule=2)
[1] 10.0 10.0  8.0  7.5  7.0  8.0  9.0 10.0 11.0

Instead of the initial 10, i want it to be 12 (difference between the 2nd and the 3rd elements carried to the previous interpolation). There is the combination of Shift's I could use, after applying the na.approx without na.rm option. But is there an Na.approx or an approx functionality that I could use?

ashleych
  • 1,042
  • 8
  • 25
  • You could use `Hmisc::approxExtrap` to fill-out/extrapolate the outward problem `NA`s. – r2evans Oct 02 '17 at 05:37
  • thanks. I also came across this https://stackoverflow.com/questions/41293458/how-to-extrapolate-values-of-panel-data-in-r. na.Spline also seems to do the job. – ashleych Oct 02 '17 at 05:48
  • `na.approx(c(NA,10,8,NA,7,NA,NA,NA,11),na.rm = FALSE, rule = 2)` will extend the closest non-NA into the leading or trailing NAs. Not precisely what you asked but maybe that is sufficient. – G. Grothendieck Oct 02 '17 at 12:32

0 Answers0