[R] Taking specific/timed differences in a zoo timeseries

Sergey Goriatchev sergeyg at gmail.com
Wed Oct 14 16:39:51 CEST 2009


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




More information about the R-help mailing list