Suggest you use chron class rather than Date class since the format you indicate is the default format for chron.
> library(zoo) > library(chron) > > set.seed(12345) > data <- round(runif(27)*10+runif(27)*5, 0) > dates <- chron(c("09/03/09", "09/04/09", "09/07/09", "09/09/09", + "09/10/09", "09/11/09", "09/14/09", "09/16/09", "09/17/09", + "09/18/09", "09/21/09", "09/22/09", "09/23/09", + "09/24/09", "09/25/09", "09/28/09", "09/29/09", "09/30/09", + "10/01/09", "10/02/09", "10/05/09", "10/06/09", "10/07/09", + "10/08/09", "10/09/09", "10/13/09", "10/14/09")) > temp <- zoo(data, order.by=dates) > > rng <- range(time(temp)) > dt <- seq(rng[1], rng[2], "day") > temp.m <- na.locf(merge(temp, zoo(, dt))) > > # create a lagged time scale and subtract the > # lagged series from original > > dt.lag <- as.chron(as.yearmon(dt)+1/12) + as.numeric(month.day.year(dt)$day) > - 1 > temp - zoo(coredata(temp.m), dt.lag) 10/05/09 10/06/09 10/07/09 10/08/09 10/09/09 10/13/09 10/14/09 -5 -6 3 2 -2 2 1 See R News 4/1 for a relevant article and also its references. On Wed, Oct 14, 2009 at 11:41 AM, Sergey Goriatchev <serg...@gmail.com> wrote: > Dear Gabor, > > Thank you very much for your help! > I'm now using your suggestion with my data. > > May I ask a stupid question? > The output's index now has format "2009-10-14". How can I transform it back > into original "10/14/09" and use this in a zoo object? > > Regards, > Sergey > > On Wed, Oct 14, 2009 at 17:03, Gabor Grothendieck <ggrothendi...@gmail.com> > wrote: >> >> Try this: >> >> library(zoo) >> # temp <- ... from post asking question >> >> # create a day sequence, dt, with no missing days >> # and create a 0 width series with those times. >> # merge that with original series giving original >> # series plus a bunch of times having NA values. >> # Use na.locf to fill in those values with the last >> # non-missing so far. >> >> rng <- range(time(temp)) >> dt <- seq(rng[1], rng[2], "day") >> >> temp.m <- na.locf(merge(temp, zoo(, dt))) >> >> # create a lagged time scale and subtract the >> # lagged series from original >> >> dt.lag <- as.Date(as.yearmon(dt)+1/12) + as.numeric(format(dt, "%d")) - 1 >> temp - zoo(coredata(temp.m), dt.lag) >> >> >> Using your data the output from the last line is: >> >> > temp - zoo(coredata(temp.m), dt.lag) >> 2009-10-05 2009-10-06 2009-10-07 2009-10-08 2009-10-09 2009-10-13 >> 2009-10-14 >> -5 -6 3 2 -2 2 >> 1 >> >> >> On Wed, Oct 14, 2009 at 10:39 AM, Sergey Goriatchev <serg...@gmail.com> >> wrote: >> > Hello everyone. >> > >> > I have a specific problem that I have difficulties to solve. >> > Assume I have a zoo object: >> > >> > set.seed(12345) >> > data <- round(runif(27)*10+runif(27)*5, 0) >> > dates <- as.Date(c("09/03/09", "09/04/09", "09/07/09", "09/09/09", >> > "09/10/09", "09/11/09", "09/14/09", "09/16/09", "09/17/09", >> > "09/18/09", "09/21/09", "09/22/09", "09/23/09", >> > "09/24/09", "09/25/09", "09/28/09", "09/29/09", "09/30/09", >> > "10/01/09", "10/02/09", "10/05/09", "10/06/09", "10/07/09", >> > "10/08/09", "10/09/09", "10/13/09", "10/14/09"), "%m/%d/%y") >> > temp <- zoo(data, order.by=dates) >> > >> > What I need to do is to take differences between say October 14th and >> > September 14, then October 13th and September 13th, that is 1 month >> > difference independent of number of days inbetween. And when there is >> > no matching date in an earlier month, like here where there is no >> > September 13th, the date should be the first preceding date, that is >> > September 11th in this example. How can I do that? >> > >> > The above is just an example, my zoo object is very big and I need to >> > take differences between years, that is between October 14th, 2009 and >> > October 14th, 2008, then Oct.13, 2009 and Oct.13, 2008, and so on. >> > Also, the time index of my zoo object has format "10/14/09" (that is >> > Oct.14, 2009), and that is the format I need to operate with and do >> > not want to change. In the example I reformated just so that I can >> > create a zoo object. >> > >> > Could some friendly person please show me how to do such a calculation? >> > >> > Thank you in advance! >> > >> > Best, >> > Sergey >> > >> > ______________________________________________ >> > R-help@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. >> > > > > > -- > I'm not young enough to know everything. /Oscar Wilde > Experience is one thing you can't get for nothing. /Oscar Wilde > When you are finished changing, you're finished. /Benjamin Franklin > Tell me and I forget, teach me and I remember, involve me and I learn. > /Benjamin Franklin > Luck is where preparation meets opportunity. /George Patten > > Kniven skärpes bara mot stenen. > ______________________________________________ R-help@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.