R obtaining rownames date using quantmod

為{幸葍}努か 提交于 2019-11-27 19:24:54

问题


Using quantmod and collecting data from Yahoo. I am trying to get the dates that are in rownames. However I am just getting NULL.

library("quantmod")
sp500 <- new.env()

getSymbols("^GSPC", env = sp500, src = "yahoo",
           from = as.Date("2008-01-04"),  to = Sys.Date())
GSPC <- get("GSPC", envir = sp500)
date1 <- rownames(GSPC)

date1
> NULL

I would be grateful for your help into getting the rowname dates into a vector.


回答1:


You need to use the index function. The xts object isn't the same as a normal data.frame, and has its own way of handling dimension names.

# Return all dates
index(GSPC)



回答2:


Your code is broken, the way it was reported in your question.

sp500 <- new.env()

getSymbols("^GSPC", env = sp500, src = "yahoo",
    from = as.Date("2008-01-04"), to = Sys.Date()) 

GSPC <- get("GSPC", envir = sp500) 

Then you can do time(GSPC), which got a method for this kind of object.



来源:https://stackoverflow.com/questions/9668239/r-obtaining-rownames-date-using-quantmod

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!