2016-02-04 35 views
0

基本上,我想用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)的列都。因素我在哪裏跳軌道

+1

請不要不要在發佈時忽略圖書館陳述。所有的代碼應該是自包含的。 –

回答

1

儘可能多地保留元數據成爲可能,將其保存爲R數據文件:

saveRDS(this.tkr, file = '~/Desktop/data.Rds') 
df2 <- readRDS('~/Desktop/data.Rds') 

這樣,

> class(df2) 
[1] "xts" "zoo" 

這種方法的不足之處在於,如果您需要與使用R之外的其他人共享數據,那麼您的數據便攜性較差,但在這種情況下聽起來不是問題。

+0

謝謝。我不記得'saveRDS'命令,但這是我需要的。不,目前至少,我不需要在R之外共享這些文件。如果我這樣做,我會根據我想要導入文件的內容來執行類似'write.cv'的操作。再次感謝。 – Ernie

1

這將在文字形式寫一個動物園對象(可移植)和讀回:

library(quantmod) 

this.tkr <- getSymbols("VNQ", from = as.Date("2015-12-01"), to = as.Date("2015-12-15"), 
    auto.assign = FALSE, return.class = "zoo") 

write.zoo(this.tkr, "TestZoo") 
zz <- read.zoo("TestZoo", header = TRUE) 

identical(this.tkr, zz) 
## [1] TRUE 

如果你有一個XTS對象首先轉換爲動物園這樣的:

library(quantmod) 

this.tkr <- getSymbols("VNQ", from = as.Date("2015-12-01"), to = as.Date("2015-12-15"), 
    auto.assign = FALSE) 

z <- as.zoo(this.tkr) 

write.zoo(z, "TestZoo") 
zz <- read.zoo("TestZoo", header = TRUE) 

identical(z, zz) 
## [1] TRUE 

x <- as.xts(zz) 
+0

我想我在我的代碼中看到了導致所有頭痛的錯誤。 auto.assign = FALSE被省略,創建額外的行並在讀回時可能是因素。感謝您的觀察。厄尼 – Ernie