Re: [R] Formating a zoo dataset .

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon, 03 Mar 2008 08:35:39 -0500

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.
>



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. Received on Mon 03 Mar 2008 - 14:02:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 03 Mar 2008 - 14:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive