我有格式的時間序列:[R轉換POSIXlt到XTS
> 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" ...
> 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"
> length(Y$Date)
[1] 174110
,我需要轉換爲XTS格式。爲了做到這一點我也做了以下內容:
date <- Y$Date
date <- as.xts(date)
> xtsible(date) #tests wheather or not the data is convertibal to format xts
[1] TRUE
但是:
> str(date)
An 'xts' object of zero-width
> length(date)
[1] 0
> head(date['2001'])
[,1]
2001-01-01 05:30:00 NA
2001-01-01 12:00:00 NA
2001-01-02 01:30:00 NA
2001-01-02 02:00:00 NA
2001-01-02 02:00:00 NA
2001-01-02 02:00:00 NA
,爲了取回數據到數據幀:
> Y$date <- date
Error in `$<-.data.frame`(`*tmp*`, "date", value = numeric(0)) :
replacement has 0 rows, data has 174110
和
> as.data.frame(date)
Error in data.frame(`coredata(x)` = c(NA_character_, NA_character_, NA_character_, :
duplicate row.names: 2001-01-02 02:00:00, ... , 2001-01-08 06:00:00, 200
In addition: Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
corrupt data frame: columns will be truncated or padded with NAs
> str(Y)
'data.frame': 174110 obs. of 17 variables:
$ Date : POSIXlt, 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" ...
$ C : chr "MA" "IN" "SI" "ID" ...
$ Event : chr "MALAY VEHICLE SALES" "Interbank Offer Rate - Percent" "Advance GDP Estimate (YoY)" "Foreign Reserves" ...
$ News : num NA NA NA NA NA NA NA NA NA NA ...
$ Growth : num 148 NA 0.3 387.2 0 ...
$ Surv.M : num NA NA NA NA NA NA NA NA NA NA ...
$ Act : num 30892 NA 10.5 29281.4 12500 ...
$ Prior : num 30744 8100 10.2 28894.2 12500 ...
$ Revised : num NA NA NA NA NA ...
$ Type : chr NA NA "%" "$B" ...
$ Freq. : chr "M" "NA" "Q" "M" ...
$ Ticker : chr "MAVSTTL Index" "IMIBOR Index" "SGAVYOY% Index" "IDGFA Index" ...
$ Period : chr "Nov" "12/31/13" "4Q" "Dec" ...
$ Category: chr "NA" "NA" "NA" "NA" ...
$ Time : chr "12:00:00 AM" "05:30:00 AM" "01:30:00 AM" "02:00:00 AM" ...
$ Country : chr "Malaysia" "India" "Singapore" "Indonesia" ...
$ date : POSIXlt, 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" ...
我不知道爲什麼我不能告訴我將數據轉換爲xts格式,然後將其返回到數據幀中。
非常感謝您的幫助。
請粘貼'str(Y)'的輸出。 –
當然。任何幫助真的很感激。 – Vincent
這裏我不明白什麼?對我來說,你似乎試圖從數據框中提取日期,創建一個這樣的日期爲'Index'的xts對象,從'xts'中提取日期,然後將它們放回到數據框中。不變。並且:「爲了將**數據返回到數據框中」;你的'xts'對象沒有任何數據,只有'Index',即你從數據框中選取的日期。也許你可以添加一兩句關於你的總體目標。乾杯。 – Henrik