1

I have in my program one class table "xts" and "zoo" which is as follows

> head(BRA$Adj.Close)

           Adj.Close
2005-01-03     25722
2005-01-04     24848
2005-01-05     24692
2005-01-06     24367
2005-01-07     24747
2005-01-10     24292

I need add the dimname "Date" in this table, resulting in

 Date         Adj.Close
2005-01-03     25722
2005-01-04     24848
2005-01-05     24692
2005-01-06     24367
2005-01-07     24747
2005-01-10     24292

how I do it ?

  • You cannot. The first "column" is not a column. See the results of `as.vector(BRA$Adj.Close)` and `index(BRA$Adj.Close)`. –  Jul 09 '15 at 01:29
  • 1
    @LegalizeIt - they're not rownames, they are a zoo index. `rownames` returns `NULL` for these data structures. – thelatemail Jul 09 '15 at 01:31

3 Answers3

5

You can't, the dates are more like rownames than columns.

You could coerce your object into a dataframe and then add acolumn like:

x <- as.data.frame(BRA$Adj.Close)
x$Date <- index(BRA$Adj.Close)
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
4

To transform an xts object to a data.frame:

  data.frame(Date = index(BRA$Adj.Close), 
             coredata(BRA$Adj.Close))
agstudy
  • 119,832
  • 17
  • 199
  • 261
0

It seems as if what you want as the column Date are actually the row names of your data frame (if it is a data frame, that is).

If so, you can make the row names a column:

yourdataframe[2] = row.names(yourdataframe)

And then rename the column:

names(yourdataframe)[2] = "Dates"

If it is not a data frame already, convert it to one.

ben_aaron
  • 1,504
  • 2
  • 19
  • 39