2013-06-29 92 views
2

我正在嘗試讀取CSV文件並將其更改爲XTS格式。但是,我遇到並使用CSV格式的日期和時間字段分列。In R如何將此CSV數據轉換爲XTS

2012.10.30,20:00,1.29610,1.29639,1.29607,1.29619,295 
2012.10.30,20:15,1.29622,1.29639,1.29587,1.29589,569 
2012.10.30,20:30,1.29590,1.29605,1.29545,1.29574,451 
2012.10.30,20:45,1.29576,1.29657,1.29576,1.29643,522 
2012.10.30,21:00,1.29643,1.29645,1.29581,1.29621,526 
2012.10.30,21:15,1.29621,1.29644,1.29599,1.29642,330 

我試圖用

euXTS <- as.xts(read.zoo(file="EURUSD15.csv", sep=",", format="%Y.%m.%d", header=FALSE)) 

拉它,但它給了我這樣的警告消息,所以我認爲不知何故,我必須附加時間戳,但我不知道這樣做的最佳方式那。

Warning message: 
In zoo(rval3, ix) : 
Some methods for 「zoo」 objects do not work if the index entries in ‘order.by’ are not unique 

回答

6

這是更好地使用read.zoo直接在動物園對象讀取你的TS,易裹挾XTS之一:

library(xts) 
ts.z <- read.zoo(text='2012.10.30,20:00,1.29610,1.29639,1.29607,1.29619,295 
2012.10.30,20:15,1.29622,1.29639,1.29587,1.29589,569 
2012.10.30,20:30,1.29590,1.29605,1.29545,1.29574,451 
2012.10.30,20:45,1.29576,1.29657,1.29576,1.29643,522 
2012.10.30,21:00,1.29643,1.29645,1.29581,1.29621,526 
2012.10.30,21:15,1.29621,1.29644,1.29599,1.29642,330', 
     sep=',',index=1:2,tz='',format="%Y.%m.%d %H:%M") 
as.xts(ts.z) 

         V3  V4  V5  V6 V7 
2012-10-30 20:00:00 1.29610 1.29639 1.29607 1.29619 295 
2012-10-30 20:15:00 1.29622 1.29639 1.29587 1.29589 569 
2012-10-30 20:30:00 1.29590 1.29605 1.29545 1.29574 451 
2012-10-30 20:45:00 1.29576 1.29657 1.29576 1.29643 522 
2012-10-30 21:00:00 1.29643 1.29645 1.29581 1.29621 526 
2012-10-30 21:15:00 1.29621 1.29644 1.29599 1.29642 330 
+0

非常感謝!那就是我需要的索引= 1:2強制它成爲一列。 – Jerry