[R] Merging Irregular Time Series With NAs

David Winsemius dwinsemius at comcast.net
Tue Jun 23 15:52:21 CEST 2009


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

Depends what you define as easy:

require(zoo)
  m[is.na(m[,1]),1] <- m[is.na(m[,1]),2]
  m[is.na(m[,2]),2] <- m[is.na(m[,2]),1]

 > m
                      aq$mid  d2$mid
2009-06-22 11:25:40 1.63755 1.63755
2009-06-22 11:25:40 1.63760 1.63760
2009-06-22 11:25:40 1.63760 1.63760
2009-06-22 11:25:41 1.63755 1.63755
2009-06-22 11:25:41 1.63755 1.63755
2009-06-22 11:25:41 1.63760 1.63760
>
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list