R数据框与日期

时间:2012-10-19 20:12:37

标签: r dataframe quantmod

我有以下格式的数据框

           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

但是命令index(DATA.FRAME)返回整数而不是日期。我应该使用什么函数来获取日期列表而不是整数?

编辑:

dput(DATA.FRAME)的输出是

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")

2 个答案:

答案 0 :(得分:2)

rownames(DATA.FRAME) #results in character vector
#[1] "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-08" "2007-01-09" "2007-01-10"

as.Date(rownames(DATA.FRAME)) #convert to date

答案 1 :(得分:2)

我不熟悉R中的命令index。看起来日期存储为数据框的rownames

我会尝试:

as.Date(rownames(DataFrameName))

或者,您可以将整数转换为R中的日期。我完全忘记了如何,但基本上您只需要一个会话因素(例如15344 = 2007年10月9日或其他内容) - 它应该在?as.Date或{ {1}}