使用R读取和写入xts格式的文件

时间:2016-02-04 22:57:19

标签: r dataframe xts zoo

基本上,我想使用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)时,列是所有因素。我在哪里跳过轨道?

2 个答案:

答案 0 :(得分:1)

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

.catch()

那样,

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

这种方法的缺点是,如果您需要与使用R之外的其他人共享数据,那么您的数据便携性会降低,但在这种情况下这听起来不是问题。

答案 1 :(得分: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)