Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R obtaining rownames date using quantmod

Tags:

r

xts

quantmod

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.

like image 651
adam.888 Avatar asked Mar 12 '12 13:03

adam.888


2 Answers

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)
like image 107
nograpes Avatar answered Nov 10 '22 16:11

nograpes


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.

like image 21
pbarill Avatar answered Nov 10 '22 18:11

pbarill