Megh Dal
2008-Mar-10 08:10 UTC
[R] Error in extracting monthly observation from a daily time series data
Hi all, Suppose I have following dataset : library(zoo) SD = 1 date1 = seq(as.Date("01/01/90", format = "%m/%d/%y"), as.Date("12/31/08", format = "%m/%d/%y"), by = 1) len1 = length(date1); data1 = zoo(matrix(rnorm(len1, mean=0, sd=SD*0.5), nrow = len1), date1) Now I want to extract monthly observation. obs = split(as.data.frame(data1), format(index(data1), "%y%m")) However surprisingly order of the observation has been changed : > head(obs, 1) $`0001` data1 2000-01-01 -0.11638271 2000-01-02 -0.69384682 2000-01-03 -1.20472671 2000-01-04 -0.29262033 2000-01-05 -0.49685794 2000-01-06 0.27514305 2000-01-07 -0.34568013 2000-01-08 0.46075677 2000-01-09 -1.37813508 2000-01-10 0.04960789 2000-01-11 0.42585954 2000-01-12 -0.12685112 2000-01-13 0.24664998 2000-01-14 0.41344957 2000-01-15 0.68487436 2000-01-16 -0.67718350 2000-01-17 -0.39434446 2000-01-18 -0.21775954 2000-01-19 0.10819901 2000-01-20 0.17013283 2000-01-21 -0.49088991 2000-01-22 0.69400376 2000-01-23 0.16209050 2000-01-24 0.03103822 2000-01-25 -0.27605458 2000-01-26 0.01629620 2000-01-27 0.61727694 2000-01-28 -0.55922333 2000-01-29 0.01503502 2000-01-30 0.77450595 2000-01-31 0.11679859 > tail(obs, 1) $`9912` data1 1999-12-01 0.249002645 1999-12-02 -0.281302740 1999-12-03 0.672597155 1999-12-04 0.486135990 1999-12-05 0.402131711 1999-12-06 -0.754141509 1999-12-07 -0.233711029 1999-12-08 -0.064699202 1999-12-09 0.399164668 1999-12-10 -0.199112521 1999-12-11 -0.422189671 1999-12-12 -0.364795664 1999-12-13 0.175806461 1999-12-14 1.273859234 1999-12-15 0.366671124 1999-12-16 -0.339057003 1999-12-17 0.073700906 1999-12-18 0.009310303 1999-12-19 -0.156223136 1999-12-20 0.177122831 1999-12-21 -0.429045076 1999-12-22 0.207632845 1999-12-23 0.765920096 1999-12-24 0.605439902 1999-12-25 -0.294758511 1999-12-26 -0.481038222 1999-12-27 -0.200035965 1999-12-28 -0.177786043 1999-12-29 0.205357694 1999-12-30 -0.528382812 1999-12-31 -0.398879255 If you compare this with my actual data then it will be clear : > head(data1, 5) 1990-01-01 -0.59800528 1990-01-02 0.84037877 1990-01-03 0.02663068 1990-01-04 -1.38561111 1990-01-05 -0.18783481 How I can sort 'obs' in proper way? Precisely I want to see 'obs' starts from 1990 only Your help will be highly appreciated. --------------------------------- [[alternative HTML version deleted]]
Gabor Grothendieck
2008-Mar-10 11:29 UTC
[R] Error in extracting monthly observation from a daily time series data
names(obs) shows the result has been sorted so use %Y instead of %y in the format statement in the split so that "2000" > "1999"; otherwise, "99" > "00". On Mon, Mar 10, 2008 at 4:10 AM, Megh Dal <megh700004 at yahoo.com> wrote:> Hi all, > > Suppose I have following dataset : > > library(zoo) > SD = 1 > date1 = seq(as.Date("01/01/90", format = "%m/%d/%y"), as.Date("12/31/08", format = "%m/%d/%y"), by = 1) > len1 = length(date1); data1 = zoo(matrix(rnorm(len1, mean=0, sd=SD*0.5), nrow = len1), date1) > > Now I want to extract monthly observation. > > obs = split(as.data.frame(data1), format(index(data1), "%y%m")) > > However surprisingly order of the observation has been changed : > > head(obs, 1) > $`0001` > data1 > 2000-01-01 -0.11638271 > 2000-01-02 -0.69384682 > 2000-01-03 -1.20472671 > 2000-01-04 -0.29262033 > 2000-01-05 -0.49685794 > 2000-01-06 0.27514305 > 2000-01-07 -0.34568013 > 2000-01-08 0.46075677 > 2000-01-09 -1.37813508 > 2000-01-10 0.04960789 > 2000-01-11 0.42585954 > 2000-01-12 -0.12685112 > 2000-01-13 0.24664998 > 2000-01-14 0.41344957 > 2000-01-15 0.68487436 > 2000-01-16 -0.67718350 > 2000-01-17 -0.39434446 > 2000-01-18 -0.21775954 > 2000-01-19 0.10819901 > 2000-01-20 0.17013283 > 2000-01-21 -0.49088991 > 2000-01-22 0.69400376 > 2000-01-23 0.16209050 > 2000-01-24 0.03103822 > 2000-01-25 -0.27605458 > 2000-01-26 0.01629620 > 2000-01-27 0.61727694 > 2000-01-28 -0.55922333 > 2000-01-29 0.01503502 > 2000-01-30 0.77450595 > 2000-01-31 0.11679859 > > > tail(obs, 1) > $`9912` > data1 > 1999-12-01 0.249002645 > 1999-12-02 -0.281302740 > 1999-12-03 0.672597155 > 1999-12-04 0.486135990 > 1999-12-05 0.402131711 > 1999-12-06 -0.754141509 > 1999-12-07 -0.233711029 > 1999-12-08 -0.064699202 > 1999-12-09 0.399164668 > 1999-12-10 -0.199112521 > 1999-12-11 -0.422189671 > 1999-12-12 -0.364795664 > 1999-12-13 0.175806461 > 1999-12-14 1.273859234 > 1999-12-15 0.366671124 > 1999-12-16 -0.339057003 > 1999-12-17 0.073700906 > 1999-12-18 0.009310303 > 1999-12-19 -0.156223136 > 1999-12-20 0.177122831 > 1999-12-21 -0.429045076 > 1999-12-22 0.207632845 > 1999-12-23 0.765920096 > 1999-12-24 0.605439902 > 1999-12-25 -0.294758511 > 1999-12-26 -0.481038222 > 1999-12-27 -0.200035965 > 1999-12-28 -0.177786043 > 1999-12-29 0.205357694 > 1999-12-30 -0.528382812 > 1999-12-31 -0.398879255 > > If you compare this with my actual data then it will be clear : > > > head(data1, 5) > > 1990-01-01 -0.59800528 > 1990-01-02 0.84037877 > 1990-01-03 0.02663068 > 1990-01-04 -1.38561111 > 1990-01-05 -0.18783481 > > How I can sort 'obs' in proper way? Precisely I want to see 'obs' starts from 1990 only > > Your help will be highly appreciated. > > > --------------------------------- > > [[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. >