Skip to content
Prev 1528 / 15274 Next

Zoo NA handling documentation?

Hmm.  In fact, there is yet another one.  We will have to bring all this
together properly but for now here is a summary.  (My previous posts
did not include na.spline.) Note that there is a help page in zoo for
?na.approx and ?na.spline  and in stinepackfor ?na.stinterp .

library(zoo)
zz <- zoo(c(NA, NA, 1:2, NA, 3:5, NA, 6, NA))
na.approx(zz)  # linear interpolation
na.contiguous(zz)  # longest stretch without NAs
na.locf(zz)   # last occurence (that is not NA) carried forward
na.omit(zz)  # omit the NAs
na.spline(zz) # spline interpolation
na.trim(zz)  # remove NAs off beginning and end but not interior ones
library(stinepack)
na.stinterp(zz) # stineman interpolation
3   4   5   6   7   8   9  10
1.0 2.0 2.5 3.0 4.0 5.0 5.5 6.0
6 7 8
3 4 5
3  4  5  6  7  8  9 10 11
 1  2  2  3  4  5  5  6  6
3  4  6  7  8 10
 1  2  3  4  5  6
1         2         3         4         5         6         7
       8         9        10        11
-4.676471 -1.058824  1.000000  2.000000  2.463235  3.000000  4.000000
5.000000  5.742647  6.000000  5.522059
3  4  5  6  7  8  9 10
 1  2 NA  3  4  5 NA  6
3        4        5        6        7        8        9       10
1.000000 2.000000 2.500000 3.000000 4.000000 5.000000 5.600269 6.000000