[R-SIG-Finance] Zoo NA handling documentation?

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jul 22 15:58:25 CEST 2007


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

> library(zoo)
> zz <- zoo(c(NA, NA, 1:2, NA, 3:5, NA, 6, NA))
> na.approx(zz)  # linear 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
> na.contiguous(zz)  # longest stretch without NAs
6 7 8
3 4 5
> na.locf(zz)   # last occurence (that is not NA) carried forward
 3  4  5  6  7  8  9 10 11
 1  2  2  3  4  5  5  6  6
> na.omit(zz)  # omit the NAs
 3  4  6  7  8 10
 1  2  3  4  5  6
> na.spline(zz) # spline interpolation
        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
> na.trim(zz)  # remove NAs off beginning and end but not interior ones
 3  4  5  6  7  8  9 10
 1  2 NA  3  4  5 NA  6
> library(stinepack)
> na.stinterp(zz) # stineman interpolation
       3        4        5        6        7        8        9       10
1.000000 2.000000 2.500000 3.000000 4.000000 5.000000 5.600269 6.000000



More information about the R-SIG-Finance mailing list