用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转换一定要让mode的列变成“character”?我看str(new.xts)的时候,列都是factor,我跑哪里去了?

要尽可能多地保留元数据,请将其保存为 R 数据文件:

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

这样,

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

这种方法的缺点是,如果您需要与使用 R 以外的东西的人共享数据,则数据的可移植性较差,但在这种情况下这听起来不是问题。

这将以文本形式(可移植地)写入一个动物园对象并读回:

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)