基本上,我想用getSymbols(quantmod)捕獲數據,將文件寫入磁盤,然後用另一個腳本將其讀回。如果可能,我想使用xts對象。我似乎無法完成這項工作。這裏是我做了什麼(及其許多變化):用xts格式讀取和寫入文件R
getSymbols("VNQ", from = as.Date("2015-12-01"), to = as.Date("2015-12-15"))
this.tkr <- get("VNQ")
head(this.tkr)
VNQ.Open VNQ.High VNQ.Low VNQ.Close VNQ.Volume VNQ.Adjusted
2015-12-01 79.50 80.52 79.42 80.49 3847300 79.38125
2015-12-02 80.26 80.37 78.73 78.85 5713500 77.76385
2015-12-03 78.73 78.85 77.40 77.61 4737300 76.54093
2015-12-04 77.68 79.29 77.65 79.09 3434100 78.00054
2015-12-07 78.96 79.19 78.52 78.87 4195100 77.78357
2015-12-08 78.44 79.09 78.36 78.80 3638600 77.71454
class(this.tkr)
[1] "xts" 「zoo"
write.zoo(this.tkr, "Data/TestZoo」)
## then in some other script ....
new.tkr <- read.table("Data/TestZoo", stringsAsFactors = FALSE)
class(new.tkr)
[1] 「data.frame"
head(new.tkr)
V1 V2 V3 V4 V5 V6 V7
1 Index VNQ.Open VNQ.High VNQ.Low VNQ.Close VNQ.Volume VNQ.Adjusted
2 2015-12-01 79.5 80.519997 79.419998 80.489998 3847300 79.381254
3 2015-12-02 80.260002 80.370003 78.730003 78.849998 5713500 77.763845
4 2015-12-03 78.730003 78.849998 77.400002 77.610001 4737300 76.540928
5 2015-12-04 77.68 79.290001 77.650002 79.089996 3434100 78.000537
6 2015-12-07 78.959999 79.190002 78.519997 78.870003 4195100 77.783574
## attempt to convert this to an xts object ...
new.tkr <- new.tkr[2:nrow(new.tkr), ] #delete first row of text captions
new.xts <- xts(new.tkr[, 2:ncol(new.tkr)], as.Date(new.tkr$V1))
head(new.xts)
V2 V3 V4 V5 V6 V7
2015-12-01 "79.5" "80.519997" "79.419998" "80.489998" "3847300" "79.381254"
2015-12-02 "80.260002" "80.370003" "78.730003" "78.849998" "5713500" "77.763845"
2015-12-03 "78.730003" "78.849998" "77.400002" "77.610001" "4737300" "76.540928"
2015-12-04 "77.68" "79.290001" "77.650002" "79.089996" "3434100" "78.000537"
2015-12-07 "78.959999" "79.190002" "78.519997" "78.870003" "4195100" "77.783574"
2015-12-08 "78.440002" "79.089996" "78.360001" "78.800003" "3638600" 「77.714538"
爲什麼XTS轉換堅持製作模式「字符「列當我看着STR(new.xts)的列都。因素我在哪裏跳軌道
請不要不要在發佈時忽略圖書館陳述。所有的代碼應該是自包含的。 –