1

我有以下形式的数据框

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

2 回答 2

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
于 2012-10-19T20:19:37.373 回答
2

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

我会尝试:

as.Date(rownames(DataFrameName))

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

于 2012-10-19T20:19:38.153 回答