Search code examples
rcsvtime-seriesxtszoo

export XTS to CSV, with separate columns


I currently have some time series data and it is in XTS form. This is some of it below.

head(dbt)
                    NSW1.Price     Coal
2018-01-01 00:30:00      91.86 71.29267
2018-01-01 01:00:00      88.83 71.25520
2018-01-01 01:30:00      73.62 71.22649
2018-01-01 02:00:00      71.49 71.18284
2018-01-01 02:30:00      69.27 71.14081
2018-01-01 03:00:00      68.44 71.10430

I am trying to export this to a CSV file, but when I do, it doesn't export into separate columns. This is what happens when I try export it.

current csv output

The code I am using is

write.zoo(dbt,file="export.csv",index.name="okay",row.names=FALSE,col.names=TRUE)

Solution

  • You need sep=",".

    Directly from the help for write.zoo:

    read.zoo and write.zoo are convenience functions for reading and writing "zoo"
    series from/to text files. They are convenience interfaces to read.table and
    write.table, respectively.
    
    
    ... further arguments passed to read.table, write.table, or read.zoo, respectively.
    

    Hence, this should work:

    write.zoo(dbt,file="export.csv",index.name="okay",row.names=FALSE,col.names=TRUE,sep=",")