I have a vector containing NA's at the boundary
x <- c(NA, -1, 1,-1, 1, NA, -1, 2, NA, NA)
I want the outcome to be:
c(-3, -1, 1,-1, 1, 0, -1, 2, 5, 8)
In other words, I want to fill both inner and boundary NA's with linear interpolation (maybe I cannot call it "inter-polation" since NA's are at boundaries).
I tried a function in the Package "zoo", na.fill(x, "extend"), but the boundary output is not something I want, which just repeats the leftmost or rightmost non-NA value:
na.fill(x,"extend")
and the output is
[1] -1 -1 1 -1 1 0 -1 2 2 2
I also checked other functions for filling NA, such as na.approx(), na.locf(), etc. but none of them works.
na.spline does work but the output of boundary NA's lead to an extremely large variation.
na.spline(x)
The output is:
[1] -15.9475983 -1.0000000 1.0000000 -1.0000000 1.0000000 0.3400655 -1.0000000 2.0000000
[9] 13.1441048 35.9323144
The boundary points are too large. Can anyone help me out? Thanks in advance!