2

I am trying to do something apparently obvious, but have no way to solve it. From a dataframe in R downloaded from the web as follows I need to save the data. Here is how I do download it:

library(tseries)
library(zoo)
ts <- get.hist.quote(instrument="DJIA", 
                     start="2008-07-01", end="2017-03-05", 
                     quote="Close", provider="yahoo", origin="1970-01-01",
                     compression="d", retclass="zoo")

Then, returns object "ts" with a two columns table; the first of dates (with no header as R prefers) and the other with the "Close" value of DJIA

            > ts
                  Close
    2008-07-01 11382.26
    2008-07-02 11215.51
    2008-07-03 11288.53
    2008-07-07 11231.96
    .
    .
    .
    2016-03-03 16943.90
    2016-03-04 17006.77 

I need this data exported in txt or similar format and import the list later; (because I will try to process health information, with no internet access) but when I try to save it; the date column with no header is missing. Additionally a "number of row" column is added. I do appologize if the question is obvious but have no other option to solve it

Mario
  • 23
  • 1
  • 4
  • Possible duplicate of [Write xts/zoo object to csv with index](http://stackoverflow.com/questions/20748721/write-xts-zoo-object-to-csv-with-index) – RHA Mar 19 '16 at 19:50

2 Answers2

1

The date column has no header, because the date is imported as rownames/index. The default of write.csv has row.names = FALSE. Try:

write.csv(ts, file = "ts.csv",row.names=TRUE)

EDIT
Strangly, this doesn't work with an object of class "zoo"

According tot ? write.table:

write.table prints its required argument x (after converting it to a data frame if it is not one nor a matrix) to a file or connection.

Apparently this conversion fails somehow. However, this works:
write.csv(data.frame(ts), file = "ts.csv",row.names=TRUE)

RHA
  • 3,677
  • 4
  • 25
  • 48
  • Yes RHA; I've tried this before; but the *.csv file has not the same data as displayed by ts in R and that's my difficulty – Mario Mar 19 '16 at 18:39
  • @Mario then please show in your question what goes wrong. Or try converting the rownames to a column. See edited answer above! – RHA Mar 19 '16 at 18:56
  • ts.csv displays the following ; ,"Close" 1,11382.259766 2,11215.509766 3,11288.530273 4,11231.959961 But it not displays the dates in standard characters 2008-07-01 11382.26 – Mario Mar 19 '16 at 19:06
  • @Mario You are right, this doesn't work for zoo objects. Posted a solution above, hope it works for you too! – RHA Mar 19 '16 at 19:47
  • YESSSSS My dear @RHA it worked fine! But it took me a lot to go nowhere; because in addition to the difficulty i am learning R Oh ; Thanks Thanks & thanks again my friend! – Mario Mar 19 '16 at 21:06
  • @Mario You're welcome. If you feel your question has been answered, then please consider accepting the answer by clicking on the accept mark left of the answer. – RHA Mar 20 '16 at 09:59
0

The ts object is a zoo object (not a two column table). In this case the zoo object is internally represented by a one column matrix of data and an "index" attribute holding the dates.

1) save/load If the only thing you want to do with the output file is to read it back into R later then there is no reason to require text and any format will do. In particular you could do this:

save(ts, file = "ts.Rda")

Now in a later session:

library(zoo)
load("ts.Rda")

1a) This would also work and produces an R source file that when sourced reconstructs the zoo object:

dump("ts", "ts.R")

and in a later session:

library(zoo)
source("ts.R")

2) write.zoo/read.zoo This will give a text file:

write.zoo(ts, "ts.dat")

and it can be written back in another session using:

library(zoo)
ts <- cbind( read.zoo("ts.dat", header = TRUE) )
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341