I am trying to use na.spline
, part of the zoo
package, to replace NA
values in some imported speed data with a cubic spline interpolated values.
na.spline
is modifying the NA
values as it is supposed to; however, it is also modifying values that originally equaled 0.
ex <- data.frame(speed)
ex$speed2 <- na.spline(ex$speed)
My data set is ~1400 values. I have included the first ~40 values below. Here you see the original speed
values and the incorrectly interpolated results in speed 2
:
speed speed2
NA 8.639277e-06
0.000000 0.000000e+00
0.000000 0.000000e+00
0.000000 0.000000e+00
0.000000 -1.694066e-21
0.000000 0.000000e+00
0.000000 -2.710505e-20
0.000000 0.000000e+00
0.000000 -4.336809e-19
0.000000 0.000000e+00
0.000000 6.938894e-18
0.000000 0.000000e+00
0.000000 1.110223e-16
2.661698 2.661698e+00
3.107128 3.107128e+00
7.319669 7.319669e+00
10.800864 1.080086e+01
17.855491 1.785549e+01
18.250267 1.825027e+01
28.587002 2.858700e+01
36.405397 3.640540e+01
38.467383 3.846738e+01
38.685956 3.868596e+01
43.917737 4.391774e+01
40.829615 4.082962e+01
43.519173 4.351917e+01
45.597497 4.559750e+01
43.252656 4.325266e+01
45.581646 4.558165e+01
48.258325 4.825832e+01
48.269969 4.826997e+01
50.905045 5.090505e+01
53.258165 5.325817e+01
58.391370 5.839137e+01
59.278440 5.927844e+01
58.720518 5.872052e+01
56.933438 5.693344e+01
62.062116 6.206212e+01
59.860849 5.986085e+01
60.183378 6.018338e+01
Has anyone seen a similar issue or have an alternative method to replace the NA
value with interpolated data?
Internally it does this (which does not involve zoo):
giving:
Also note that this would zero out output components corresponding to zero values in the input: