0

我想根据主时间序列的列索引生成时间序列列表 ( 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

因此,例如,列表中的第一个 data.frame 将由m[,1], m[,2], m[,3]并且m[,4]看起来像

              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

14 号会是这样的

              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

有没有我可以在表单中编写或使用的功能

apply(cols,2, function(x)  DOSOMETHINGLIKE"a <- merge(a,m[x])"HERE)
4

1 回答 1

2
  1. 一些样本数据:

    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. 验证class数据以查看它是否不是标准data.frame

    class(TS)
    # [1] "timeSeries"
    # attr(,"package")
    # [1] "timeSeries"
    
  3. 制作一个示例矩阵,我们将从上面创建的 TS 对象中选择列:

    GetMe <- replicate(3, sample(5, 3))
    GetMe
    #      [,1] [,2] [,3]
    # [1,]    5    1    3
    # [2,]    2    4    1
    # [3,]    4    2    4
    
  4. 用于lapply根据上述矩阵的列值进行子集化:

    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. 验证class此列表中对象的 es:

    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. 将来 请提供一个可重复的示例,并在您的问题中具体说明您实际使用的内容(例如,您用于创建数据的包、当前加载的内容等)。您的问题清楚地表明您正在使用data.frames,但您对我的原始答案的评论表明您实际上正在使用包中的一个timeSeries对象,该对象timeSeries需要(稍微)不同的方法。

于 2012-11-03T16:27:50.973 回答