[R] From daily series to monthly and viceversa
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Apr 18 21:24:40 CEST 2009
Try this:
library(zoo)
# z is CPI. Just use 1, 2, 3, ... for example.
z <- zooreg(1:12, as.yearmon("2008-01"), freq = 12)
# z2 contains z and lags of z and days.in.month
# It has a Date class time index, rather than yearmon.
days.in.month <- as.numeric(as.Date(time(z), frac = 1) - as.Date(time(z)) + 1)
z2 <- cbind(z, z1 = lag(z, -1), z2 = lag(z, -2), z3 = lag(z, -3), days.in.month)
time(z2) <- as.Date(time(z2))
# z3 is z2 expanded to include each day of each month and
# day of the month that each row represents
dd <- seq(time(z2)[1], as.Date(as.yearmon(tail(time(z2), 1)), frac = 1), "day")
z3 <- na.locf(cbind(z2, zoo(, dd)))
z3$day.of.month <- as.numeric(format(time(z3), "%d"))
# now each row of z3 has all the data you need so apply(z3, 1, your.function)
On Sat, Apr 18, 2009 at 2:28 PM, manta <mantino84 at libero.it> wrote:
>
> Well Gabor, this is actually a really good help, thanks so much.
> There is only one problem, I'm getting what you say, but in the code there
> are a couple of errors
>
> time(z2) <- as.Date(time(zz)) #probably z2
> z3 <- na.locf(
> cbind(zz,
> zoo(, dd), #dd object not found
> day.of.month = as.numeric(format(time(zz), "%d"))
> ))
> z3$day.of.month <- as.numeric(format(time(zz2), "%d")) #z2?
>
> And why did you call partial this solution?
> Thanks again for you great help
> Take care
>
>
> Gabor Grothendieck wrote:
>>
>> Here is a partial solution:
>>
>> library(zoo)
>> # z is CPI. Just use 1, 2, 3, ... for example.
>> z <- zooreg(1:12, as.yearmon("2008-01"), freq = 12)
>> days.in.month <- as.numeric(as.Date(time(z), frac = 1) - as.Date(time(z))
>> + 1)
>> z2 <- cbind(z, z1 = lag(z, -1), z2 = lag(z, -2), z3 = lag(z, -3),
>> days.in.month)
>> time(z2) <- as.Date(time(zz))
>> z3 <- na.locf(
>> cbind(zz,
>> zoo(, dd),
>> day.of.month = as.numeric(format(time(zz), "%d"))
>> ))
>> z3$day.of.month <- as.numeric(format(time(zz2), "%d"))
>> # now each row of z3 has all the data you need so apply(z3, 1,
>> your.function)
>>
>>
>> On Fri, Apr 17, 2009 at 2:13 PM, manta <mantino84 at libero.it> wrote:
>>>
>>> any update anybody? I'm really stucked!
>>> --
>>> View this message in context:
>>> http://www.nabble.com/From-daily-series-to-monthly-and-viceversa-tp23064454p23103052.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>> ______________________________________________
>> 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.
>>
>>
>
> --
> View this message in context: http://www.nabble.com/From-daily-series-to-monthly-and-viceversa-tp23064454p23115847.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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