xts对象数据和data.frame对象数据似乎根本不匹配

时间:2016-07-05 14:54:53

标签: r csv xts quantmod

我正在获取xts对象获取一些任意数字并从无处拾取调整后的数据列。我错过了什么?

library(quantmod)

DIB <- read.table(text="Date Symbols Open High  Low Close  Volume
2015-07-05     DIB 6.81 6.87 6.80  6.83  587524
2015-07-06     DIB 6.75 6.86 6.74  6.82 1839035
2015-07-07     DIB 6.80 6.84 6.78  6.80 3206400
2015-07-08     DIB 6.84 6.85 6.75  6.85 5069482
2015-07-09     DIB 6.82 6.82 6.74  6.78 1644686
2015-07-12     DIB 6.92 6.94 6.80  6.80 2183164", header=TRUE, as.is=TRUE)
write.csv(DIB, "DIB.csv", row.names=FALSE)

getSymbols(Symbols="DIB", env=globalenv(),  return.class="xts", extension="DIB.csv")
# [1] "DIB"

head(DIB)
#            DIB.Open DIB.High DIB.Low DIB.Close DIB.Volume DIB.Adjusted
# 2010-06-07     2.02     2.04    2.02      2.04    1318700      1.39143
# 2010-06-08     2.05     2.07    2.02      2.03    1647000      1.38461
# 2010-06-09     2.05     2.06    2.02      2.02    1923600      1.37779
# 2010-06-10     2.03     2.03    2.00      2.00    2485100      1.36415
# 2010-06-14     2.05     2.06    2.03      2.05    1946800      1.39825
# 2010-06-15     2.02     2.03    2.00      2.02    4991600      1.37779

1 个答案:

答案 0 :(得分:0)

您需要在src="csv"来电中指定getSymbols,否则默认会调用getSymbols.yahoo

dib <- read.table(text="Date Symbols Open High  Low Close  Volume
2015-07-05     DIB 6.81 6.87 6.80  6.83  587524
2015-07-06     DIB 6.75 6.86 6.74  6.82 1839035
2015-07-07     DIB 6.80 6.84 6.78  6.80 3206400
2015-07-08     DIB 6.84 6.85 6.75  6.85 5069482
2015-07-09     DIB 6.82 6.82 6.74  6.78 1644686
2015-07-12     DIB 6.92 6.94 6.80  6.80 2183164", header=TRUE, as.is=TRUE)
# remove the "Symbols" column to avoid an all character xts object
write.csv(dib[,-2L], "DIB.csv", row.names=FALSE)

getSymbols("DIB", src="csv", col.names=c("Open","High","Low","Close","Volume"))
DIB
#            DIB.Open DIB.High DIB.Low DIB.Close DIB.Volume
# 2015-07-05     6.81     6.87    6.80      6.83     587524
# 2015-07-06     6.75     6.86    6.74      6.82    1839035
# 2015-07-07     6.80     6.84    6.78      6.80    3206400
# 2015-07-08     6.84     6.85    6.75      6.85    5069482
# 2015-07-09     6.82     6.82    6.74      6.78    1644686
# 2015-07-12     6.92     6.94    6.80      6.80    2183164
unlink("DIB.csv")