[R-SIG-Finance] Zoo: na.locf, na.approx behaviour changes between versions 1.6.3 and 1.6.4

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jan 6 14:05:27 CET 2011


On Thu, Jan 6, 2011 at 5:34 AM, Giuseppe Milicia
<Giuseppe.Milicia at makoglobal.com> wrote:
> Guys,
>
> I'd like to point out a behavior change in na.locf (and na.approx) from the zoo package between versions 1.6.3 and 1.6.4
>
> If na.rm=T (it is by default) in 1.6.3 na.omit would be used to remove the leading NAa, in 1.6.4 na.trim is used instead. In zoo 1.6.4
>
> tmp = zoo(as.matrix(1, ncol=1, nrow=100), 1:100)
> colnames(tmp)="TEST"
> class(coredata(tmp))
> [1] "matrix"
> colnames(tmp)
> [1] "TEST"
> colnames(na.locf(tmp))
> NULL
> class(coredata(na.locf(tmp)))
> [1] "numeric"
> class(tmp) = c("Test",class(tmp))
> class(na.locf(tmp))
> [1] "zoo"
>
> Whilst with zoo 1.6.3 the univariate zoo series keep the matrix coredata and the colnames :(, it also kept the class of the original.
>
> Unfortunately this breaks my code :(
>
> Any particular motivation between this change? I'm downgrading to 1.6.3 for the time being :(
>
> Cheers,
>

There seems to be referring to two different things here:

1. It drops a dimension if the input is a zoo matrix with one column
and na.rm = TRUE. That is a bug in na.trim.  Its fixed in the
development version of zoo:

>   library(zoo)
>   source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/na.trim.R?revision=816&root=zoo")
>   z <- zoo(cbind(x = c(NA, 1, NA, 3)))
>   na.locf(z)
  x
2 1
3 1
4 3

Alternately here is a workaround that would work if maxgap= is not used:

> z <- zoo(cbind(x = c(NA, 1, NA, 3)))
> na.omit(na.locf(z, na.rm = FALSE))
  x
2 1
3 1
4 3
attr(,"na.action")
[1] 1
attr(,"class")
[1] "omit"

2. na.trim is used instead of na.omit.  How is this a problem since
na.locf fills in NAs so there will be no NAs in the result anyways
unless possibly if you use the new maxgap argument (and in that case
you don't want certain internal NAs removed)?

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-SIG-Finance mailing list