5

I converted a zoo time series into a data frame in R and the date became the index of the data frame. Is there a way to have the date represented as a normal column in the data frame?

monthly_df <- data.frame(monthly_zoo)

head(monthly_zoo)

zoo output

head(monthly_df)

dataframe output

MFR
  • 2,049
  • 3
  • 29
  • 53
SteveJones22
  • 61
  • 1
  • 1
  • 5
  • `monthly_df$month <- rownames(monthly_df)` or use `tibble::rownames_to_column` – alistaire Oct 19 '16 at 02:18
  • 3
    Please **[edit]** your post and show the actual code / input / output as text instead of screenshots. Others can't copy and paste from your images. [See here](http://meta.stackoverflow.com/a/285557/1402846) for details. Thank you. – Pang Oct 19 '16 at 02:22

2 Answers2

10

fortify.zoo(z) converts zoo object z to a data.frame with a first column equal to the index.

library(zoo)
z <- zoo(1:3, as.Date("2000-01-01") + 0:2) # test object
fortify.zoo(z)

giving:

       Index z
1 2000-01-01 1
2 2000-01-02 2
3 2000-01-03 3

If ggplot2 is loaded (so that the fortify generic is present) then it can alternately be written as:

library(ggplot2)
fortify(z)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
6

You want as.data.frame(). Witness:

R> library(quantmod)
Loading required package: xts
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
R> IBM <- as.zoo(getSymbols("IBM"))  # convert from xts
R> class(IBM)
[1] "zoo"
R> tail(IBM)
           IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
2016-10-11   156.73   156.95  153.89    154.79    2901300       154.79
2016-10-12   154.97   154.97  153.08    154.29    2964000       154.29
2016-10-13   153.70   154.22  152.27    153.72    2909900       153.72
2016-10-14   154.47   155.53  154.09    154.45    4358200       154.45
2016-10-17   154.45   155.89  154.34    154.77    5890400       154.77
2016-10-18   150.02   151.00  147.79    150.72   12705700       150.72
R> as.data.frame(tail(IBM))
           IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
2016-10-11   156.73   156.95  153.89    154.79    2901300       154.79
2016-10-12   154.97   154.97  153.08    154.29    2964000       154.29
2016-10-13   153.70   154.22  152.27    153.72    2909900       153.72
2016-10-14   154.47   155.53  154.09    154.45    4358200       154.45
2016-10-17   154.45   155.89  154.34    154.77    5890400       154.77
2016-10-18   150.02   151.00  147.79    150.72   12705700       150.72
R> class(as.data.frame(tail(IBM)))
[1] "data.frame"
R> 

To add the date as a column (instead of relying on the default rownames) make it explicit:

R> IBM <- getSymbols("IBM")  # keep as xts
R> tail(data.frame(index(IBM), as.data.frame(IBM)))
           index.IBM. IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
2016-10-11 2016-10-11   156.73   156.95  153.89    154.79    2901300       154.79
2016-10-12 2016-10-12   154.97   154.97  153.08    154.29    2964000       154.29
2016-10-13 2016-10-13   153.70   154.22  152.27    153.72    2909900       153.72
2016-10-14 2016-10-14   154.47   155.53  154.09    154.45    4358200       154.45
2016-10-17 2016-10-17   154.45   155.89  154.34    154.77    5890400       154.77
2016-10-18 2016-10-18   150.02   151.00  147.79    150.72   12705700       150.72
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • I might be misinterpreting, but doesn't this still just leave the `index` as `row.names` and not as a "proper" column? – thelatemail Oct 19 '16 at 02:21
  • This seems cleaner: `cbind( index(IBM), data.frame(IBM))` I've learned to distrust `as.data.frame`. – IRTFM Oct 19 '16 at 04:40