簡體   English   中英

使用R以xts格式讀取和寫入文件

[英]Reading and writing files in xts format with R

基本上,我想使用getSymbols(quantmod)捕獲數據,將文件寫入磁盤,然后使用另一個腳本讀回它們。 如果可能,我想使用xts對象。 我似乎無法完成這項工作。 這是我所做的(及其許多變體):

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)時,所有列都是因素,我在哪里跳轉?

要保留盡可能多的元數據,請將其保存為R數據文件:

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

那樣,

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

這種方法的缺點是,如果您需要與使用R以外的其他人共享數據,則數據的可移植性較差,但是在這種情況下,這聽起來像不是一個問題。

這將以文本形式(便攜式)編寫一個Zoo對象並讀回它:

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對象,請先將其轉換為zoo,如下所示:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM