[R-SIG-Finance] Combine two incomplete zoo object (with NAs) in one zoo series

Joshua Ulrich josh.m.ulrich at gmail.com
Thu Apr 1 15:34:55 CEST 2010


Pierre,

?ifelse says:
"Value:

     A vector of the same length and attributes (including class) as
     ‘test’ and data values from the values of ‘yes’ or
     ‘no’."

...so it won't return a zoo object.

Try this instead:
> Z <- z[,2]
> Z[is.na(Z)] <- z[is.na(Z),1]
> Z
1992-01-10 1992-01-17 1992-01-24 1992-01-31 1992-02-07
        11         12         13         14         15
1992-02-14 1992-02-21 1992-02-28 1992-03-06 1992-03-13
        16         17          8          9         10

HTH,
--
Joshua Ulrich
FOSS Trading: www.fosstrading.com



On Thu, Apr 1, 2010 at 8:12 AM, Pierre Lapointe <pierrelap at gmail.com> wrote:
> Hello I have two times series for the same asset. Unfortunately, the series
> with the most history has been discontinued. I want to combine the old
> series with the new one in a new zoo object. I tried combining them using
> ifelse, but the result is a vector, not a zoo object. In other words, I lost
> the dates in the process.
>
> How do I get the result in a zoo object?
>
> Here's my attempt:
>
>
>
> #build series for reproducible example
>
> z <- zoo(matrix(1:20,ncol=2),
>
>     as.Date(c("1992-01-10", "1992-01-17", "1992-01-24", "1992-01-31",
>
>       "1992-02-07", "1992-02-14", "1992-02-21", "1992-02-28", "1992-03-06",
>
>       "1992-03-13")))
>
> z[1:5,1] <-NA
>
> z[8:10,2] <-NA
>
>
>
> #initial series with imcompletes
>
> z
>
>
>
> #my function
>
> bc.combine <-function(imcomplete.1,fill.with.2){
>
> res <-ifelse(!is.na(imcomplete.1),imcomplete.1,fill.with.2)
>
> return(res)
>
> }
>
>
>
> #two zoo objects as inputs but the result is a vector
>
> bc.combine(z[,2,drop=F],z[,1,drop=F])
>
>
>
>      [,1]
>
>  [1,]   11
>
>  [2,]   12
>
>  [3,]   13
>
>  [4,]   14
>
>  [5,]   15
>
>  [6,]   16
>
>  [7,]   17
>
>  [8,]    8
>
>  [9,]    9
>
> [10,]   10
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>



More information about the R-SIG-Finance mailing list