[R] setting up dates

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 16 19:06:05 CEST 2010


On Fri, Jul 16, 2010 at 12:38 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> I have several data sets, which begin early in 2002 and run until
> yesterday.  They do not have observations every day.  For example:
>> xd1[1:10]
> 2002-02-25 2002-02-26 2002-02-28 2002-03-01 2002-03-04 2002-03-05 2002-03-07
>         7          8          1          9         12          3          5
> 2002-03-08 2002-03-11 2002-03-12
>         7         10          5
>>
>
>
> I want to set up zoo series which has every day from 2002-01-01
> through yesterday.  There should be zeros on the "non appearing" dates
> and the previously seen values on the appearing dates.
>
> I've tried union and merge, but they didn't seem to work correctly.
>

Try this:

z <- zoo(c(7, 8, 1, 9, 12, 3, 5, 7, 10, 5),
   as.Date(c("2002-02-25", "2002-02-26", "2002-02-28", "2002-03-01",
      "2002-03-04", "2002-03-05", "2002-03-07", "2002-03-08", "2002-03-11",
      "2002-03-12")))

dd <- seq(start(z), end(z), "day")
merge(z, zoo(,dd), fill = 0)

Please try displaying your data using dput to make it easier to copy
it into R.  For example,
dput(z)



More information about the R-help mailing list