我无法让 xts 识别行名中的时间序列
I cannot get xts to recognize Time Series in rownames
我的 xts 包有问题。我正在尝试从数据帧创建一个 xts。为了简单起见,我尝试在下面的小 df 上复制我正在尝试做的事情:
> df <- japanTOPIX[,1, drop = FALSE]
> typeof(df[,1])
[1] "double"
> typeof(rownames(df))
[1] "character"
> head(df, 3)
X7164.JT.Equity
12/27/2000 65.0
12/28/2000 66.5
12/29/2000 66.2
> head(as.Date(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27" "2000-12-28" "2000-12-29"
> timeBased(as.Date(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.Date(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.Date(rownames(df), format = "%m/%d/%Y")) :
order.by requires an appropriate time-based object
> head(strptime(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(strptime(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = strptime(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = strptime(rownames(df), format = "%m/%d/%Y")) :
order.by requires an appropriate time-based object
> head(as.POSIXlt(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(as.POSIXlt(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.POSIXlt(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.POSIXlt(rownames(df), format = "%m/%d/%Y")) :
order.by requires an appropriate time-based object
> head(as.POSIXct(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(as.POSIXct(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.POSIXct(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.POSIXct(rownames(df), format = "%m/%d/%Y")) :
order.by requires an appropriate time-based object
如您所见,我每次尝试创建此 xts 时都遇到错误。该包声称我不是按基于时间的对象排序,但 'by = ' 变量实际上是基于时间的。有帮助吗??
(如果示例或代码不清楚,请告诉我,我会尽力澄清。)
编辑:我认为这可能与行名中的一些非唯一值有关,但事实并非如此:
> nrow(df)
[1] 5115
> length(unique(rownames(df)))
[1] 5115
您错误地使用了 xts
函数。第二个参数应该是 order.by
而不是 by
。使用您的数据查看以下示例:
数据:
df <- read.table(text='
X7164.JT.Equity
12/27/2000 65.0
12/28/2000 66.5
12/29/2000 66.2')
解决方案:
> xts(df, order.by = as.Date(rownames(df), format = "%m/%d/%Y"))
X7164.JT.Equity
2000-12-27 65.0
2000-12-28 66.5
2000-12-29 66.2
如您所见,它按预期工作!
P.S。除了 as.Date
,您还可以使用 as.POSIXct
或 as.POSIXlt
或 strptime
。所有这些都正常工作!
我的 xts 包有问题。我正在尝试从数据帧创建一个 xts。为了简单起见,我尝试在下面的小 df 上复制我正在尝试做的事情:
> df <- japanTOPIX[,1, drop = FALSE]
> typeof(df[,1])
[1] "double"
> typeof(rownames(df))
[1] "character"
> head(df, 3)
X7164.JT.Equity
12/27/2000 65.0
12/28/2000 66.5
12/29/2000 66.2
> head(as.Date(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27" "2000-12-28" "2000-12-29"
> timeBased(as.Date(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.Date(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.Date(rownames(df), format = "%m/%d/%Y")) :
order.by requires an appropriate time-based object
> head(strptime(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(strptime(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = strptime(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = strptime(rownames(df), format = "%m/%d/%Y")) :
order.by requires an appropriate time-based object
> head(as.POSIXlt(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(as.POSIXlt(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.POSIXlt(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.POSIXlt(rownames(df), format = "%m/%d/%Y")) :
order.by requires an appropriate time-based object
> head(as.POSIXct(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(as.POSIXct(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.POSIXct(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.POSIXct(rownames(df), format = "%m/%d/%Y")) :
order.by requires an appropriate time-based object
如您所见,我每次尝试创建此 xts 时都遇到错误。该包声称我不是按基于时间的对象排序,但 'by = ' 变量实际上是基于时间的。有帮助吗?? (如果示例或代码不清楚,请告诉我,我会尽力澄清。)
编辑:我认为这可能与行名中的一些非唯一值有关,但事实并非如此:
> nrow(df)
[1] 5115
> length(unique(rownames(df)))
[1] 5115
您错误地使用了 xts
函数。第二个参数应该是 order.by
而不是 by
。使用您的数据查看以下示例:
数据:
df <- read.table(text='
X7164.JT.Equity
12/27/2000 65.0
12/28/2000 66.5
12/29/2000 66.2')
解决方案:
> xts(df, order.by = as.Date(rownames(df), format = "%m/%d/%Y"))
X7164.JT.Equity
2000-12-27 65.0
2000-12-28 66.5
2000-12-29 66.2
如您所见,它按预期工作!
P.S。除了 as.Date
,您还可以使用 as.POSIXct
或 as.POSIXlt
或 strptime
。所有这些都正常工作!