3

using the quantmod package, I am pulling stock data, as below

library(quantmod)
getSymbols('F')
head(F)

which gives the output

           F.Open F.High F.Low F.Close F.Volume
2007-01-03   7.56   7.67  7.44    7.51 78671500
2007-01-04   7.56   7.72  7.43    7.70 63545800
2007-01-05   7.72   7.75  7.57    7.62 40563800
2007-01-08   7.63   7.75  7.62    7.73 48941200
2007-01-09   7.75   7.86  7.73    7.79 56732500
2007-01-10   7.79   7.79  7.67    7.73 42398600
# and an unimportant(here) warning regarding download length

I want to be able to extract the obvious date column here, and work with the data as a data.frame, usually I would try to look up the column names and pull that column, but the dates are not included in a column!

Liz Young
  • 408
  • 5
  • 17
  • sorry did not see that one. desired answer here with data.frame might be different enough? – Liz Young Jul 04 '15 at 07:29
  • Let others decide whether they considered it is a duplicate or not. –  Jul 04 '15 at 07:31
  • Duplicates often end up getting more hits than the originals because the way the problem is formulated is different. My understanding is they are not supposed to be deleted, unless they are obvious copies. – Mike Wise Jul 04 '15 at 07:59

2 Answers2

6

You could try

  dates <- index(F)

The quantmod package extracts the data in xts format. It might not always be advisable to convert an xts time series into a dataframe.

RHertel
  • 23,412
  • 5
  • 38
  • 64
  • 1
    It is good to have the right way of using it here too. – Mike Wise Jul 04 '15 at 09:08
  • No offense intended concerning your solution, which offers an answer that corresponds better to the description in the title of the OP. It's just that one loses many if not all functionalities provided by the quantmod package after such a format conversion. – RHertel Jul 04 '15 at 10:55
  • In complete agreement. But somethings do require data frames and one needs to be aware of both possibilities. – Mike Wise Jul 04 '15 at 11:04
4

It is not a data.frame, it is an xts. If you want to manipulate it as a data.frame and get the dates (they come over as rownames) try:

   df <- data.frame(F)
   row.names(df)
Mike Wise
  • 22,131
  • 8
  • 81
  • 104