R:Quantmod在股票代码前拉动带有^符号的标准普尔指数



我在使用quantmod包获取S&P(股票代码=^GSPC(。

对于普通股票(例如:DFS(,我可以运行以下代码:

start <- as.Date("2020-01-01")
end <- as.Date("2020-05-06")
getSymbols("DFS", src = "yahoo", from = start, to = end)
DFS <-  data.frame(date=index(DFS), coredata(DFS))
head(DFS)

我对S&P是名称中的^符号。

start <- as.Date("2020-01-01")
end <- as.Date("2020-05-06")
getSymbols("^GSPC", src = "yahoo", from = start, to = end)
SP500 <-  data.frame(date=index(^GSPC), coredata(^GSPC))

此代码给出以下错误:
错误:"SP500<-data.frame(date=index(^(">中出现意外的"^">

有人知道这方面的好办法吗?

使用auto.assign = FALSE。这允许您将自己的值分配给自己的data.frame,并使用更合适的名称。

start <- as.Date("2020-01-01")
end <- as.Date("2020-05-06")
df <- quantmod::getSymbols("^GSPC", src = "yahoo", from = start, to = end, auto.assign = FALSE)

顺便说一句,这也将是quantmod未来更新中的默认行为。

最新更新