Search code examples
rquantmod

Read CSV in R, but Keep Character Row Names


I am using the popular R library "quantmod" and cannot figure out how to keep the character row names when I read the file to a csv.

For instance, I will have some data as follows:

ROW NAME       VALUE
1970-05-08     .05
1970-08-01     .05
1970-12-10     .06
...            ---

When I use

write.csv(MyData,'MyData.csv', row.names = T)

The output looks like this:

Column One     Column Two
1               .05
2               .05
3               .06
...             ---

How do I keep the character row name? I.e. how can value 1 in the csv actually read as 1970-05-08?

Thanks!


Solution

  • You have to be aware that quantmod returns xts-objects. So the first column (the dates) is the index. To write xts-objects to a, say csv-file, the easiest way is to use the write.zoo function.

    getSymbols('AAPL',from='2018-01-01’)
    write.zoo(AAPL,'aapl.csv',sep=',',quote=FALSE)
    

    quotes=FALSE removes the quotes around the column names.