rory.winston at gmail.com
2009-Jun-23 13:29 UTC
[R] Merging Irregular Time Series With NAs
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]]
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. >
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
On Jun 23, 2009, at 9:50 AM, Gabor Grothendieck wrote:> Your zoo object is illegal since zoo objects are not supposed to > have duplicated times;They were unique at the sub-second level.> 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.63760But that will not give the desired answer if both aq$mid and d2$mid were present at the same time.> > > 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 >>David Winsemius, MD Heritage Laboratories West Hartford, CT
On Tue, Jun 23, 2009 at 10:04 AM, David Winsemius<dwinsemius at comcast.net> wrote:> > On Jun 23, 2009, at 9:50 AM, Gabor Grothendieck wrote: > >> Your zoo object is illegal since zoo objects are not supposed to >> have duplicated times; > > They were unique at the sub-second level. > >> 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 > > But that will not give the desired answer if both aq$mid and d2$mid were > present at the same time.What was written was: "if the object has at most one NA in each row, which is the case in the example, ..."
On Tue, Jun 23, 2009 at 10:10 AM, Gabor Grothendieck<ggrothendieck at gmail.com> wrote:> On Tue, Jun 23, 2009 at 10:04 AM, David Winsemius<dwinsemius at comcast.net> wrote: >> >> On Jun 23, 2009, at 9:50 AM, Gabor Grothendieck wrote: >> >>> Your zoo object is illegal since zoo objects are not supposed to >>> have duplicated times; >> >> They were unique at the sub-second level. >> >>> 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 >> >> But that will not give the desired answer if both aq$mid and d2$mid were >> present at the same time. > > What was written was: > "if the object has at most one NA in each row, which is the case in > the example, ..." >hit send too quickly. continuing: but what was meant was at most one non-NA.
Maybe Matching Threads
- "table(droplevels(aq)$Month)" in manual page of droplevels
- Odd behaviour in within.list() when deleting 2+ variables
- Odd behaviour in within.list() when deleting 2+ variables
- IVT SCD support status
- "table(droplevels(aq)$Month)" in manual page of droplevels