Search code examples
rdataframequantmod

R data frame with dates


I have a data frame in the following form

           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
2007-01-03   142.25   142.86  140.57    141.37   94807600       125.38
2007-01-04   141.23   142.05  140.61    141.67   69620600       125.65
2007-01-05   141.33   141.40  140.38    140.54   76645300       124.64
2007-01-08   140.82   141.41  140.25    141.19   71655000       125.22
2007-01-09   141.31   141.60  140.40    141.07   75680100       125.11
2007-01-10   140.58   141.57  140.30    141.54   72428000       125.53

however the command index(DATA.FRAME) return integers rather than dates. What function should I use to get a list of dates instead of integers?

EDIT:

The output of dput(DATA.FRAME) is

structure(list(SPY.Open = c(142.25, 141.23, 141.33, 140.82, 141.31, 
140.58), SPY.High = c(142.86, 142.05, 141.4, 141.41, 141.6, 141.57
), SPY.Low = c(140.57, 140.61, 140.38, 140.25, 140.4, 140.3), 
    SPY.Close = c(141.37, 141.67, 140.54, 141.19, 141.07, 141.54
    ), SPY.Volume = c(94807600, 69620600, 76645300, 71655000, 
    75680100, 72428000), SPY.Adjusted = c(125.38, 125.65, 124.64, 
    125.22, 125.11, 125.53)), .Names = c("SPY.Open", "SPY.High", 
"SPY.Low", "SPY.Close", "SPY.Volume", "SPY.Adjusted"), row.names = c("2007-01-03", 
"2007-01-04", "2007-01-05", "2007-01-08", "2007-01-09", "2007-01-10"
), class = "data.frame")

Solution

  • I'm not familiar with the command index in R. It looks like the dates are stored as the rownames of your data frame.

    I would try:

    as.Date(rownames(DataFrameName))
    

    Alternatively, you can turn integers into dates in R. I forget exactly how, but basically you just need one conversation factor (say 15344 = Oct. 9th 2007 or something) - it should be in ?as.Date or ?as.POSIXct