0

I would like to generate a list of time series based on the column indexs of a master time series (m)

head(cols)
          [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
    [1,]    1    1    1    1    1    1    1    1    1     1     1     1     1     1     1     1     1     1
    [2,]    2    2    2    2    2    2    2    2    2     2     2     2     2     2     2     2     2     2
    [3,]    3    3    3    3    3    3    3    3    4     4     4     4     4     4     4     5     5     5
    [4,]    4    5    6    7    8    9   10   11    5     6     7     8     9    10    11     6     7     8

so for example the first data.frame in the list would be made from m[,1], m[,2], m[,3] and m[,4] and look something like

              1            2             3            4   
2012-01-01 -0.0248361511  0.0127908458 -0.011976191  0.009987137
2012-01-02 -0.0005351887  0.0290350115  0.004208001  0.007078312
2012-01-03 -0.0016072867  0.0042660695  0.008660648 -0.018255748
2012-01-04  0.0016072867 -0.0001303243  0.001782532 -0.004775416
2012-01-05  0.0026730837 -0.0038740336 -0.007149271  0.015511091

And the 14th would be something like

              1            2             4            10
2012-01-01 -0.0248361511  0.0127908458  0.009987137  0.0051973431
2012-01-02 -0.0005351887  0.0290350115  0.007078312  0.0081517268
2012-01-03 -0.0016072867  0.0042660695 -0.018255748 -0.0008121889
2012-01-04  0.0016072867 -0.0001303243 -0.004775416  0.0071366761
2012-01-05  0.0026730837 -0.0038740336  0.015511091  0.0186782999

is there some function I can write or use in the form

apply(cols,2, function(x)  DOSOMETHINGLIKE"a <- merge(a,m[x])"HERE)
lab_notes
  • 407
  • 5
  • 11

1 Answers1

2
  1. Some sample data:

    set.seed(1)
    setRmetricsOptions(myFinCenter = "GMT")
    charvec = timeCalendar()
    TS <- as.timeSeries(matrix(rnorm(60), ncol = 5), charvec)
    head(TS)
    # GMT
    #                  TS.1        TS.2        TS.3       TS.4       TS.5
    # 2012-01-01 -0.6264538 -0.62124058  0.61982575 -0.3942900 -0.1123462
    # 2012-02-01  0.1836433 -2.21469989 -0.05612874 -0.0593134  0.8811077
    # 2012-03-01 -0.8356286  1.12493092 -0.15579551  1.1000254  0.3981059
    # 2012-04-01  1.5952808 -0.04493361 -1.47075238  0.7631757 -0.6120264
    # 2012-05-01  0.3295078 -0.01619026 -0.47815006 -0.1645236  0.3411197
    # 2012-06-01 -0.8204684  0.94383621  0.41794156 -0.2533617 -1.1293631   
    
  2. Verify the class of the data to see that it is not a standard data.frame:

    class(TS)
    # [1] "timeSeries"
    # attr(,"package")
    # [1] "timeSeries"
    
  3. Make a sample matrix from which we will be selecting the columns from the TS object created above:

    GetMe <- replicate(3, sample(5, 3))
    GetMe
    #      [,1] [,2] [,3]
    # [1,]    5    1    3
    # [2,]    2    4    1
    # [3,]    4    2    4
    
  4. Use lapply to subset according to the column-wise values from the above matrix:

    myList <- lapply(1:ncol(GetMe), function(x) TS[, GetMe[, x]])
    myList[[1]] # View one of the resulting subsetted data
    # GMT
    #                  TS.5        TS.2       TS.4
    # 2012-01-01 -0.1123462 -0.62124058 -0.3942900
    # 2012-02-01  0.8811077 -2.21469989 -0.0593134
    # 2012-03-01  0.3981059  1.12493092  1.1000254
    # 2012-04-01 -0.6120264 -0.04493361  0.7631757
    # 2012-05-01  0.3411197 -0.01619026 -0.1645236
    # 2012-06-01 -1.1293631  0.94383621 -0.2533617
    # 2012-07-01  1.4330237  0.82122120  0.6969634
    # 2012-08-01  1.9803999  0.59390132  0.5566632
    # 2012-09-01 -0.3672215  0.91897737 -0.6887557
    # 2012-10-01 -1.0441346  0.78213630 -0.7074952
    # 2012-11-01  0.5697196  0.07456498  0.3645820
    # 2012-12-01 -0.1350546 -1.98935170  0.7685329
    
  5. Verify the classes of the objects in this list:

    lapply(myList, class)
    # [[1]]
    # [1] "timeSeries"
    # attr(,"package")
    # [1] "timeSeries"
    # 
    # [[2]]
    # [1] "timeSeries"
    # attr(,"package")
    # [1] "timeSeries"
    # 
    # [[3]]
    # [1] "timeSeries"
    # attr(,"package")
    # [1] "timeSeries"
    
  6. In the future please provide a reproducible example and be specific in your question about what you are actually working with (for example, which packages you have used to create your data, what is currently loaded, and so on). Your question clearly states you are working with data.frames, but your comment to my original answer reveals you're actually working with a timeSeries object from the timeSeries package which requires a (slightly) different approach.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • This works as a data.frame but I was hoping to avoid the conversation back and forth between a timeseries and data.frame – lab_notes Nov 03 '12 at 16:38
  • @lab_notes, What do you mean by "a timeseries"? What is the structure of your equivalent of "myDF" in this example? If you post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) you are more likely to get more useful answers. – A5C1D2H2I1M1N2O1R2T1 Nov 03 '12 at 16:42
  • as in timeseries from http://cran.r-project.org/web/packages/timeSeries/timeSeries.pdf – lab_notes Nov 03 '12 at 16:49
  • @lab_notes, that comment should really be a part of your original question since there are different packages that deal with time series data in R, including functions in base R. It is always good to specify in your questions what packages are being used; if you can also share a sample dataset, that would be very helpful. – A5C1D2H2I1M1N2O1R2T1 Nov 03 '12 at 17:16
  • Thanks very much, In future I will provide reproducible examples. – lab_notes Nov 03 '12 at 19:21