【问题标题】:Getting last 100 value of a ts object in R在 R 中获取 ts 对象的最后 100 个值
【发布时间】:2016-08-17 13:38:56
【问题描述】:

我有一个数据如下。

cotton <- structure(list(V1 = c(52L, 49L, 49L, 44L, 47L, 52L, 45L, 51L, 
                                54L, 57L, 67L, 71L, 66L, 65L, 75L, 66L, 70L, 70L, 69L, 71L, 70L, 
                                72L, 73L, 73L, 75L, 69L, 77L, 75L, 71L, 74L, 69L, 71L, 70L, 78L, 
                                74L, 72L, 74L, 72L, 73L, 73L, 72L, 70L, 73L, 71L, 76L, 79L, 68L, 
                                79L, 76L, 78L, 78L, 78L, 75L, 75L, 73L, 78L, 78L, 78L, 81L, 80L, 
                                79L, 84L, 82L, 81L, 80L, 83L, 77L, 81L, 82L, 83L, 82L, 86L, 78L, 
                                82L, 81L, 79L, 79L, 80L, 75L, 78L, 78L, 77L, 80L, 80L, 80L, 82L, 
                                81L, 84L, 83L, 82L, 84L, 81L, 80L, 83L, 87L, 81L, 84L, 84L, 82L, 
                                84L, 83L, 84L, 82L, 80L, 78L, 84L, 84L, 84L, 82L, 84L, 79L, 82L, 
                                79L, 79L, 72L, 73L, 78L, 82L, 83L, 81L, 77L, 75L, 70L, 71L, 66L, 
                                59L, 57L, 62L, 60L, 58L, 59L, 56L, 53L, 55L, 56L, 57L, 62L, 58L, 
                                56L, 60L, 63L, 66L, 71L, 74L, 70L, 74L, 75L, 74L, 77L, 79L, 76L, 
                                77L, 79L, 80L, 81L, 78L, 77L, 78L, 77L, 75L, 71L, 66L, 63L, 57L, 
                                55L, 55L, 55L, 54L, 57L, 57L, 53L, 54L, 60L, 63L, 65L, 64L, 68L, 
                                74L, 73L, 74L, 75L, 73L, 77L, 75L, 76L, 68L, 73L, 49L, 69L, 80L, 
                                82L, 78L, 71L, 70L, 73L, 71L, 68L, 72L, 70L, 43L, 72L, 81L, 81L, 
                                80L, 73L, 73L, 72L, 68L, 71L, 73L, 67L, 43L, 68L, 69L, 77L, 78L
)), .Names = "V1", class = "data.frame", row.names = c(NA, -216L
))

我想分析这个数据框的最后 100 个值。我使用 ts() 函数将其转换为具有已知开始和频率的时间序列对象。但是当我取最后 100 个值如下时,

cotton.ts <- ts(cotton, start = 2000, frequency = 12)
if(length(cotton.ts) > 100 ){
      cotton.ts <- cotton.ts[(length(cotton.ts)-99):length(cotton.ts)]
    }

cotton.ts 成为一个向量。我需要它是一个时间序列,但频率和开始年份总是在变化。所以我不想一直寻找新的开始年份和月份来让它再次成为时间序列。有没有办法在不浪费时间的情况下做到这一点?

【问题讨论】:

  • 无论是时间序列还是数据框,tail(cotton, 100)都应该这样做。
  • @RHertel 它给了我最后 100 个值,但不是时间序列。

标签: r time-series


【解决方案1】:

我不想一直寻找新的开始年份和月份来重新制作时间序列。有没有办法在不浪费时间的情况下做到这一点?

恐怕你必须这样做。反正也不难。下面是一种自动方式

u <- length(cotton.ts) - 99
ts(cotton.ts[u:length(cotton.ts)], start = c(2000 + floor(u / 12), u %% 12),
   frequency = 12)

#     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#2009                                  78  82  83  81
#2010  77  75  70  71  66  59  57  62  60  58  59  56
#2011  53  55  56  57  62  58  56  60  63  66  71  74
#2012  70  74  75  74  77  79  76  77  79  80  81  78
#2013  77  78  77  75  71  66  63  57  55  55  55  54
#2014  57  57  53  54  60  63  65  64  68  74  73  74
#2015  75  73  77  75  76  68  73  49  69  80  82  78
#2016  71  70  73  71  68  72  70  43  72  81  81  80
#2017  73  73  72  68  71  73  67  43  68  69  77  78

注意start = c(a, b)ts() 中的使用。

【讨论】:

    【解决方案2】:

    另一种可能的解决方案:

    #convert cotton.ts into xts object
    cotton.xts <- as.xts(coredata(cotton.ts), order.by = timeBasedSeq('2000/2017/m'))
    

    现在您可以使用它的函数last,它返回最后一个n periods。 例如获取最近 7 个月:

    > xts::last(cotton.xts,7)
             V1
    Jun 2017 73
    Jul 2017 67
    Aug 2017 43
    Sep 2017 68
    Oct 2017 69
    Nov 2017 77
    Dec 2017 78
    

    【讨论】:

      【解决方案3】:

      您可以使用 tail() 函数获得一个值的最后 100 个值。也许它也适用于 ts:

      tail(data, n=100)
      

      编辑

      getTime(head(cotton.ts,u))
      

      其中 X - 您的时间序列对象, n——时间点的位置。 您可以在 R 中的 timeSeries 包中找到该函数。

      它似乎有效。

      【讨论】:

      • 你试过getTime()了吗?作为第二个参数,您可以指定系列中的位置。
      • @ali 看看我的回答。
      • 不。不工作。我按照您的建议进行了尝试,但错误为 getTime(cotton.ts, u) 中的错误:未使用的参数 (u)。我试过 getTime(cotton.ts[u]) 但也没有用。其中 u 是第 n 个元素。
      • 我想我明白了。试试看:getTime(head(cotton.ts,u))
      猜你喜欢
      • 2019-04-03
      • 2021-02-11
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多