[R-SIG-Finance] Replacing some observation of a zoo object

Gabor Grothendieck ggrothendieck at gmail.com
Fri Mar 4 14:57:10 CET 2011


On Fri, Mar 4, 2011 at 6:10 AM, Bogaso <bogaso.christofer at gmail.com> wrote:
> Hi all, I having some problem in replacing some observation of a zoo object
> with NA. Here I have tries followings:
>
>> MyData <- zooreg(rnorm(60), start=as.Date("2011-01-01"))[-2]
>> MyData[as.Date(c("2011-01-01", "2011-01-02", "2011-01-03"))] <- NA  ## I
>> like to replace values with those indices with 'NA'
>> MyData
> Error in dimnames(x) <- dn :
>  length of 'dimnames' [1] not equal to array extent
>
> Can somebody point me on the correct approach?

This is a bug that has been fixed in the development version of zoo. Try this:

library(zoo)
source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/zoo.R?revision=807&root=zoo")
MyData <- zooreg(rnorm(60), start=as.Date("2011-01-01"))[-2]
MyData[as.Date(c("2011-01-01", "2011-01-02", "2011-01-03"))] <- NA

This gives:

> MyData
 2011-01-01  2011-01-02  2011-01-03  2011-01-04  2011-01-05  2011-01-06
         NA          NA          NA -1.34805886 -1.91697046  1.25480605
 2011-01-07  2011-01-08  2011-01-09  2011-01-10  2011-01-11  2011-01-12
-1.11093108 -0.09022114 -1.48167745  0.09454079  0.14537740  3.20595170
 2011-01-13  2011-01-14  2011-01-15  2011-01-16  2011-01-17  2011-01-18
 1.76102208  0.30314129  0.80446039 -0.44131485  0.30540308 -0.06544116
 2011-01-19  2011-01-20  2011-01-21  2011-01-22  2011-01-23  2011-01-24
 0.51936876  1.09788586 -0.44658700  0.34662045 -0.90035035  0.70918434
 2011-01-25  2011-01-26  2011-01-27  2011-01-28  2011-01-29  2011-01-30
-1.48424386  0.03681397 -1.41516001 -0.57138922  0.73444079  1.11011085
 2011-01-31  2011-02-01  2011-02-02  2011-02-03  2011-02-04  2011-02-05
-0.15998173  0.95090759  0.30755803  0.26189967  1.16055471 -0.67093163
 2011-02-06  2011-02-07  2011-02-08  2011-02-09  2011-02-10  2011-02-11
-0.42165283 -0.88252925 -2.77272381 -0.64737290 -0.68938798 -0.86060754
 2011-02-12  2011-02-13  2011-02-14  2011-02-15  2011-02-16  2011-02-17
-0.59200348 -1.52368460 -0.93473316 -0.04775729  0.29883692 -1.09869497
 2011-02-18  2011-02-19  2011-02-20  2011-02-21  2011-02-22  2011-02-23
-0.09645265 -1.46670669 -0.58237332  0.71437046  2.18060192 -1.24251170
 2011-02-24  2011-02-25  2011-02-26  2011-02-27  2011-02-28  2011-03-01
-0.60004701  0.76253189 -0.33870264  0.43248596  0.80008573  0.37263523

-- 
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