[R] irregular time series

Gabor Grothendieck ggrothendieck at gmail.com
Wed Sep 12 18:42:33 CEST 2007


Try this.  After defining our data, tt and x, we create a zoo series
z by aggregating over days taking the last point corresponding to
each day.  Then we convert that to ts (which fills in the missing days)
and create a zoo series with no data and those times merging it
with the  original zoo series and specifying fill = 0.

library(zoo)

# create zoo series, z, by taking last point corresponding to each day
tt <- c("2003-03-05", "2003-03-05", "2003-05-05" ,"2003-04-07" ,"2003-03-05")
x <- 1:5
z <- aggregate(zoo(x), as.Date(tt), tail, 1)

merge(z, zoo(, time(as.ts(z))), fill = 0)

# alternative to last line is the following: longer but no warning
# only change is inserting of as.Date(unclass(...)) to force Date class
merge(z, zoo(, as.Date(unclass(time(as.ts(z))))), fill = 0)


On 9/12/07, Nirav Mehta <niravmehta.x at gmail.com> wrote:
> Howdy!
>
> I am attempting to convert a date frame with irregular dates into a
> regular time series, aggregated by date. i have tried using both the
> 'its' and 'zoo' packages.
>
> I have something like
>
> times<-c("2003-03-05", "2003-03-05", "2003-05-05" ,"2003-04-07" ,"2003-03-05")
> aarf<-data.frame(times)
> aarf$x<-runif(5)
>
> what i want in the end, is a daily time series, aggregating x by day,
> and filling in days where there are no data with 0.
>
> I have tried, similarly to another help request, the aggregating once
> I have zoo-class data, but my dates disappear, and instead I get
> things like 12772 and so on for the various dates. I thought that
> these 5-digit numbers were maybe the number of days past Jan 1, 1970,
> but dividing these numbers by the number of days in a year gets me out
> of my data range.
>
> Also, applying as.ts() to my zoo data gives me a regularly spaced time
> series that ignores the timing imposed in zoo.
>
> Similarly, I can convert the series to an 'its' object, but then
> cannot convert the 'its' object to a regular time series with 0 filled
> in where I have no observations, since the 'its' arithmitic only
> returns the intersection of addition of two its vectors.
>
> Thanks,
> Nirav Mehta
>
> ______________________________________________
> 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