获取R的ts对象的最后100个值

问题描述:

我有一个数据如下。获取R的ts对象的最后100个值

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成为一个向量。我需要它是一个时间序列,但频率和开始年份总是在变化。所以我不想一直寻找新的开始年份和月份,让它再次成为时间序列。有没有办法做到这一点,而不会失去时间?

+0

无论是时间序列或数据帧,'尾(棉,100)'应该做的。 – RHertel

+0

@RHertel它给了我最后的100个值,但不是一个时间序列。 –

我不想再一次找到新的开始年份和月份,以使其再次成为时间序列。有没有办法做到这一点,而不会失去时间?

恐怕你必须这样做。无论如何,这并不困难。下面是一个自动的方式

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 

注意在ts()使用start = c(a, b)

您可以使用tail()函数获取的值的最后100个值。也许这也将为TS工作:

tail(data, n=100) 

编辑

getTime(head(cotton.ts,u)) 

其中X - 时间序列对象, N - 时间点的位置。 你可以在R的timeSeries包中找到这个功能。

它似乎工作。

+0

nope。再次变成整数向量。 –

+0

您是否尝试过getTime()?作为第二个参数,您可以指定系列中的位置。 –

+0

不,我不知道。你能编辑你的答案吗? –

另一种可能的解决方案:

#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