[R] Question on "Zoo" object

Gabor Grothendieck ggrothendieck at gmail.com
Wed Feb 24 13:45:14 CET 2010


See interspersed comments and be sure to read the three vignettes that
come with zoo:
vignette("zoo")
vignette("zoo-quickref")
vignette("zoo-faq")

On Wed, Feb 24, 2010 at 4:52 AM, Bogaso <bogaso.christofer at gmail.com> wrote:
>
> Dear all, I would like to ask two questions on handling "zoo" object
>
> 1. Suppose I have following matrix :
>
>  mat1 <- matrix(rnorm(60), nrow=12)
>
> Now I want to create a zoo object with index should represent the month
> names for each row. For example first row has index "jan", 2nd row is "feb"
> and so on.

> mat <- matrix(1:60, 12)
> library(zoo)
> z <- zoo(mat, factor(month.abb, levels = month.abb)); z

Jan  1 13 25 37 49
Feb  2 14 26 38 50
Mar  3 15 27 39 51
Apr  4 16 28 40 52
May  5 17 29 41 53
Jun  6 18 30 42 54
Jul  7 19 31 43 55
Aug  8 20 32 44 56
Sep  9 21 33 45 57
Oct 10 22 34 46 58
Nov 11 23 35 47 59
Dec 12 24 36 48 60

> # or perhaps using yearmon class
> zym <- zooreg(mat, start = as.yearmon("2010-01"), freq = 12); zym

Jan 2010  1 13 25 37 49
Feb 2010  2 14 26 38 50
Mar 2010  3 15 27 39 51
Apr 2010  4 16 28 40 52
May 2010  5 17 29 41 53
Jun 2010  6 18 30 42 54
Jul 2010  7 19 31 43 55
Aug 2010  8 20 32 44 56
Sep 2010  9 21 33 45 57
Oct 2010 10 22 34 46 58
Nov 2010 11 23 35 47 59
Dec 2010 12 24 36 48 60

>
>
> 2. Again I have a daily time series (zoo object) like
>
> daily.dat <- zooreg(rnorm(500), start=as.Date("2000-01-01"), frequency=1)
>
> Now I want to aggregate the data with standard deviation for each month.
> What I mean is that I want to create a zoo time series vector with length
> 12, wherein 1st element would represent the SD for "ALL" daily data
> corresponding Jan, 2nd is for SD from all data corresponding Feb, and so on
> upto Dec.
>
> I anyone please help me how to tackle them, I would be grateful.

> set.seed(1)
> daily.dat <- zooreg(rnorm(500), start=as.Date("2000-01-01"), frequency=1)
> aggregate(daily.dat, as.yearmon, sd)
 Jan 2000  Feb 2000  Mar 2000  Apr 2000  May 2000  Jun 2000  Jul 2000  Aug 2000
0.9370553 0.7743405 0.9514569 0.8813805 0.9188229 1.0969453 1.0327717 1.0392682
 Sep 2000  Oct 2000  Nov 2000  Dec 2000  Jan 2001  Feb 2001  Mar 2001  Apr 2001
1.0276672 1.0173960 0.8953093 1.0964024 0.9696928 0.9507952 1.1275021 1.1410237
 May 2001
1.5286202



More information about the R-help mailing list