将日期格式为 YYYYMMDD 的数据转换为 R 中的 xts
Convert data into xts in R with date format YYYYMMDD
我有一个名为 ff5
的时间序列 table 使用 read.csv
导入到 R
,date
列格式为 "YYYYMMDD" .
我安装了 xts
包以更好地处理时间序列数据。我尝试使用以下代码将原始数据 ff5
转换为 xts
格式:
library(xts)
ff5_xts <- xts(ff5, order.by = as.Date(ff5["date"], "%Y%m%d"))
我收到了这条错误信息:
Error in as.Date.default(x, ...) : do not know how to convert 'x'
to class “Date”
我尝试了一些有或没有 xts
的其他方法,但无法弄清楚如何将此原始数据转换为时间序列。
非常感谢任何帮助!
这个有用吗?
ff5 <- data.frame(date=c("20180615", "20180617", "20180616"))
ff5$date <- as.Date(ff5$date, "%Y%m%d")
library(xts)
ff5_xts <- xts(ff5, order.by = ff5$date)
ff5_xts
date
2018-06-15 "2018-06-15"
2018-06-16 "2018-06-16"
2018-06-17 "2018-06-17"
我有一个名为 ff5
的时间序列 table 使用 read.csv
导入到 R
,date
列格式为 "YYYYMMDD" .
我安装了 xts
包以更好地处理时间序列数据。我尝试使用以下代码将原始数据 ff5
转换为 xts
格式:
library(xts)
ff5_xts <- xts(ff5, order.by = as.Date(ff5["date"], "%Y%m%d"))
我收到了这条错误信息:
Error in as.Date.default(x, ...) : do not know how to convert 'x' to class “Date”
我尝试了一些有或没有 xts
的其他方法,但无法弄清楚如何将此原始数据转换为时间序列。
非常感谢任何帮助!
这个有用吗?
ff5 <- data.frame(date=c("20180615", "20180617", "20180616"))
ff5$date <- as.Date(ff5$date, "%Y%m%d")
library(xts)
ff5_xts <- xts(ff5, order.by = ff5$date)
ff5_xts
date
2018-06-15 "2018-06-15"
2018-06-16 "2018-06-16"
2018-06-17 "2018-06-17"