[R] Merging Irregular Time Series With NAs

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jun 23 15:50:07 CEST 2009


Your zoo object is illegal since zoo objects are not supposed to
have duplicated times; however, ignoring that if the object has
at most one NA in each row, which is the case in the example,
then this will produce a numeric vector and you can create a
new zoo object out of that:

> rowSums(z, na.rm = TRUE)
[1] 1.63755 1.63760 1.63760 1.63755 1.63755 1.63760


On Tue, Jun 23, 2009 at 9:29 AM, <rory.winston at gmail.com> wrote:
> Hi
>
> I have two irregular time series, which are of different lengths and being
> and end at different times. For the common subset of time that they both
> span, they should have the same values, but the values may occur at
> slightly different time intervals. I am trying to "line up" the identical
> values and reconcile them. I have merged the two series into a zoo object
> which looks like the following:
>
>> head(m)
> aq$mid d2$mid
> 2009-06-22 16:25:40.044 NA 1.63755
> 2009-06-22 16:25:40.909 1.63760 NA
> 2009-06-22 16:25:40.987 NA 1.63760
> 2009-06-22 16:25:41.657 1.63755 NA
> 2009-06-22 16:25:41.738 NA 1.63755
> 2009-06-22 16:25:41.909 1.63760 NA
>
> What I would like to do is merge the series column-wise : ie where one
> column contains NAs, replace the NAs with the non-NA value from the other
> column. Is this possible easily?
>
> Here is a dput() of the data above:
>
> structure(c(NA, 1.6376, NA, 1.63755, NA, 1.6376, 1.63755, NA,
> 1.6376, NA, 1.63755, NA), .Dim = c(6L, 2L), .Dimnames = list(
> NULL, c("aq$mid", "d2$mid")), index = structure(c(1245684340.044,
> 1245684340.909, 1245684340.987, 1245684341.657, 1245684341.738,
> 1245684341.909), class = c("POSIXt", "POSIXct")), class = "zoo")
>
>
> Cheers
> -- Rory
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list