2016-08-17 56 views
0

我有一個數據如下。獲取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個值,但不是一個時間序列。 –

回答

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 

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

0

您可以使用tail()函數獲取的值的最後100個值。也許這也將爲TS工作:

tail(data, n=100) 

編輯

getTime(head(cotton.ts,u)) 

其中X - 時間序列對象, N - 時間點的位置。 你可以在R的timeSeries包中找到這個功能。

它似乎工作。

+0

nope。再次變成整數向量。 –

+0

您是否嘗試過getTime()?作爲第二個參數,您可以指定系列中的位置。 –

+0

不,我不知道。你能編輯你的答案嗎? –

1

另一種可能的解決方案:

#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