我已将雅虎的DJI历史数据作为csv下载,以便在R中进行进一步分析。出于好奇getSymbols("^DJI")
似乎没有用,但我离题了。
重点是我不知道如何将此csv
文件转换为时间序列格式。
目前为止的输出和问题是:
> DJI = read.csv("^DJI.csv")
> head(DJI)
Date Open High Low Close Adj.Close Volume
1 1/29/1985 1277.72 1295.49 1266.89 1292.62 1292.62 13560000
2 1/30/1985 1297.37 1305.10 1278.93 1287.88 1287.88 16820000
3 1/31/1985 1283.24 1293.40 1272.64 1286.77 1286.77 14070000
4 2/1/1985 1276.94 1286.11 1269.77 1277.72 1277.72 10980000
5 2/4/1985 1272.08 1294.94 1268.99 1290.08 1290.08 11630000
6 2/5/1985 1294.06 1301.13 1278.60 1285.23 1285.23 13800000
> chartSeries(DJI)
Error in try.xts(x, error = "chartSeries requires an xtsible object") :
chartSeries requires an xtsible object
因此{quantmod}
函数chartSerie
正在请求.xts
个文件,但Date
中的DJI
列未立即被识别为:
> DJI = as.Date(DJI$Date)
Error in charToDate(x) :
character string is not in a standard unambiguous format
在下面的答案后编辑:
> head(DJI)
Open High Low Close Adj.Close Volume
1985-01-29 1277.72 1295.49 1266.89 1292.62 1292.62 13560000
1985-01-30 1297.37 1305.10 1278.93 1287.88 1287.88 16820000
1985-01-31 1283.24 1293.40 1272.64 1286.77 1286.77 14070000
1985-02-01 1276.94 1286.11 1269.77 1277.72 1277.72 10980000
1985-02-04 1272.08 1294.94 1268.99 1290.08 1290.08 11630000
1985-02-05 1294.06 1301.13 1278.60 1285.23 1285.23 13800000
> is.ts(DJI)
[1] FALSE
答案 0 :(得分:1)
要转换日期,您需要format
声明...
DJI$Date <- as.Date(DJI$Date,format="%m/%d/%Y")
quantmod
需要xts
个对象中的日期为行名而不是单独的列。你也应该这样做
rownames(DJI) <- DJI$Date
DJI$Date <- NULL #to remove the column
chartSeries(DJI)