0

I have a time series of four years data. And I want to do a for loop operation over the months. I am using 'xts' package. This is how first four rows of my original data frame ('obs') looks like

           t       V2     V3     V4     V5     V6     V7     V8     V9     
1 2009-09-25 18:00:00 32.965 32.965 33.525 32.965 32.965 32.965 32.965  
2 2009-09-26 06:00:00 32.965 32.965 32.965 32.965 31.853 32.407 31.301 
3 2009-09-26 18:00:00 31.853 31.853 31.853 32.407 31.301 30.752 31.301  
4 2009-09-27 06:00:00 29.687 29.194 30.752 28.707 27.750 27.279 27.279  

First I split the data series in to monthly data using

obsl <- split(obs, f = "months", drop=FALSE, k = 1)

Now I have all monthly data series in a list. I want to call each monthly list using obsl[i] 'i' being the 'i'th month list and convert this list in to matrix of monthly data series. Foe eg this is the output of obsl[2] and it's a list

                          V2      V3      V4      V5      V6      V7      V8      V9     
 2009-10-01 06:00:00  25.900  25.900  25.900  26.355  26.814  25.900  25.900  25.451  
 2009-10-01 18:00:00  25.900  25.900  25.900  25.007  25.007  25.007  25.900  25.451  
 2009-10-02 06:00:00  25.007  25.007  25.007  25.007  25.007  25.007  25.007  25.007  
 2009-10-02 18:00:00  25.007  25.007  25.007  25.007  25.900  25.007  24.136  24.569  



 2009-10-31 18:00:00  36.950  37.531  38.115  38.702  39.884  40.480  41.681  42.286  

But I am not sure if it's the correct and elegant way of doing it. Also I don't know how to convert the list to a matrix with the original matrix form (i.e first column-time stamp and the rest - numeric). Can anybody help?

tonytonov
  • 25,060
  • 16
  • 82
  • 98
rm167
  • 1,185
  • 2
  • 10
  • 26
  • As I get, `obsl` is a "list" of "data.frames"? What exactly do you want to apply to each element-data.frame in the "list"? Perhaps, you could add a sample "list" and the respective output? – alexis_laz Mar 28 '14 at 12:25
  • I added the sample list @alexis_laz. Yeah. obsl is list of xts objects and I want to apply a long 'for-loop' over the monthly matrix. But I have the monthly lists. I don't know how to proceed from here. – rm167 Mar 28 '14 at 13:06
  • 1
    Hm, I'm not sure, but perhaps you are -indeed- stuck in the difference between `[[` and `[`? E.g. if you use `obsl[[2]]` you should get the second element of the list which is a "data.frame" (and not a "matrix"). Besides that, you might want to take a look at `?lapply`. – alexis_laz Mar 28 '14 at 13:14

1 Answers1

0

To go from a list of xts objects to a single xts object, you can use the do.call(rbind, ...) idiom

obs <- getSymbols("SPY", from="2013-01-01", to="2013-12-31", src="yahoo", auto.assign=FALSE)
obsl <- split(obs, "months")
do.call(rbind, obsl)

The last line is equivalent to rbind(obsl[[1]], obsl[[2]], ..., obsl[[12]])

If this is too slow, you can try do.call.rbind

library(qmao)  # R-Forge
do.call.rbind(obsl)
Community
  • 1
  • 1
GSee
  • 48,880
  • 13
  • 125
  • 145