[R] Formating a zoo dataset .
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Mar 3 14:35:39 CET 2008
Your code below changes the class of the time index from "Date" to "character".
Perhaps you want "yearmon" class. The following averages all values in
each month producing a series with time class "yearmon". See ?yearmon
library(zoo)
z <- zoo(c(0.1423065, -0.03276228, -0.0652789, -0.04999873,
-0.01447902, 0.22265729), as.Date(c(11047, 11075, 11108,
11138, 11169, 11200)))
z
aggregate(z, as.yearmon, mean)
# Its also possible, though normally undesirable, to use "character" as your
# index class but then for it to make sense you will need to choose a
# representation which sorts appropriately under the usual rules for sorting:
aggregate(z, format(time(z), "%Y-%m"), mean)
On Mon, Mar 3, 2008 at 2:13 AM, Megh Dal <megh700004 at yahoo.com> wrote:
> Suppose I have following dataset :
> > head(data1)
> Date Return
> 1 03/31/00 0.14230650
> 2 04/28/00 -0.03276228
> 3 05/31/00 -0.06527890
> 4 06/30/00 -0.04999873
> 5 07/31/00 -0.01447902
> 6 08/31/00 0.22265729
>
> Now I convert it to zoo object :
>
> > data11 = zoo(data1[,2], as.Date(data1[,1], format="%m/%d/%y"))
> > head(data11)
> 2000-03-31 2000-04-28 2000-05-31 2000-06-30 2000-07-31 2000-08-31
> 0.14230650 -0.03276228 -0.06527890 -0.04999873 -0.01447902 0.22265729
>
> Clearly those are monthly data. Therefore I want to convert it to mm-yy format. I used following code :
> data111 = zoo(coredata(data11), format(index(data11), "%m/%y"))
>
> However what I got is that :
> > head(data111)
> 01/01 01/02 01/03 01/04 01/05 01/06
> -0.001388889 -0.016274826 -0.047707664 0.001104362 -0.077961541 0.017637141
> > tail(data111)
> 12/02 12/03 12/04 12/05 12/06 12/07
> 0.058660676 -0.018067833 -0.055569851 0.007142888 0.051162052 0.052643733
>
> It is evident that month order has been changed. Can anyone here tell me how to get correct order like :
>
> 01/01, 02/01, 03/01..................
>
> Your help is highly appreciated
>
> Regards,
>
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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