1
votes

I feel that I am possibly not understanding a table type. I'm pulling data using the package "quantmod" and function getSymbols("AAPL", src = "yahoo"). The data shows up in my grid and is usable within R.

I'm trying to export via write.csv(), but the dates are turned into ascending numbers (1,2,3,...). Is there a way to export the date as well when it is a record name?

This is what I'm using:

write.csv(AAPL, "C:/Users/Desktop/AAPL.csv", row.names = TRUE)

R shows:

Date - Price

CSV in excel shows:

1 - Price

Any help is much appreciated!

2

2 Answers

0
votes

So when you import the data for AAPL it has dates in rownames thats why when you try to export it, it doesnt include the date. USE THIS

getSymbols("AAPL", src = "yahoo")

After importing use the code below and then export x:

x<-data.frame(AAPL)
x$date<-rownames(x)
rownames(x)<-NULL
0
votes

quantmod::getSymbols() returns an xts object by default. xts objects are based on zoo objects, which are a matrix with an 'index' attribute. The date you see when xts/zoo objects are printed is the index attribute, not row names (or record names).

The easiest way to write xts/zoo objects to a text file is with write.zoo(). It will automatically include the index in the first column of the file.

write.zoo(AAPL, "aapl.csv", sep = ",")
readLines("aapl.csv", n = 5)
[1] "\"Index\",\"AAPL.Open\",\"AAPL.High\",\"AAPL.Low\",\"AAPL.Close\",\"AAPL.Volume\",\"AAPL.Adjusted\""
[2] "2007-01-03,12.327143,12.368571,11.7,11.971429,309579900,8.073009"                                   
[3] "2007-01-04,12.007143,12.278571,11.974286,12.237143,211815100,8.252192"                              
[4] "2007-01-05,12.252857,12.314285,12.057143,12.15,208685400,8.193427"                                  
[5] "2007-01-08,12.28,12.361428,12.182858,12.21,199276700,8.233888"