2013-11-25 91 views
0

我有一個TS具有以下屬性:簡單的時間間隔

summary(Y$Date) 
       Min.    1st Qu.    Median     Mean    3rd Qu.     Max. 
"2001-01-01 05:30:00" "2004-03-15 10:40:30" "2007-01-03 04:00:00" "2006-11-11 15:53:11" "2009-08-13 12:00:00" "2011-12-30 12:30:00" 

str(Y$Date) 
POSIXlt[1:174110], format: "2001-01-01 12:00:00" "2001-01-01 05:30:00" "2001-01-02 01:30:00" "2001-01-02 02:00:00" "2001-01-02 02:00:00" "2001-01-02 02:01:00" "2001-01-02 04:00:00" "2001-01-02 04:00:00" ... 

length(Y$Date) 
[1] 174110 

Y$Date[1:5] 
[1] "2001-01-01 12:00:00" "2001-01-01 05:30:00" "2001-01-02 01:30:00" "2001-01-02 02:00:00" "2001-01-02 02:00:00" 

我正在尋找一種簡單的方式來創建呈現相同的結果的時間間隔:

Y$Date[grep("2001",Y$Date)] 

中提取的所有值從2001年,只有在形式:

Y$Date["2001" =< Y$Date < "2002"] #or 
Y$Date["2001" =< Y$Date & Y$Date < "2002"] #for that matter. 

,因爲這將讓我設定的時間間隔,如從2001年至2005年等

打字:

Y$Date[Y$Date < "2002"] 

變爲R重新調整整個series.I已經使用切()拆分成TS幾個區間:

y<-cut(Y$Date,breaks="year") #and 
as.data.frame(table(y)) 

都這樣做一個相當不錯的工作,但是確實給我提供了我想要的靈活性水平。

您的幫助非常感謝,請讓我知道,如果我能以任何方式幫助您解決此問題。

非常感謝。

回答

1

轉換日期xts類對象看看?xts的例子library(xts)

date['2007'] # all of 2007 
date['2007-03/'] # March 2007 to the end of the data set 
date['2007-03/2007'] # March 2007 to the end of 2007 
date['/'] # the whole data set 
date['/2007'] # the beginning of the data through 2007 
date['2007-01-03'] # just the 3rd of January 2007 
+0

感謝:

date <- as.xts(Y$Date, descr='my new xts object') 

現在,您可以按以下格式(從?xts拍攝)提取時間間隔你的前鋒爲你的快速反應。我遇到了xts包,並已快速查看它。我很難相信在基本R中沒有其他選擇,我必須爲一個相當簡單的任務安裝額外的軟件包。 – Vincent