【问题标题】:Time Series object error when ordering by date按日期排序时出现时间序列对象错误
【发布时间】:2021-03-01 06:03:57
【问题描述】:

我在 SO 上找到了对这个问题的回答,但我仍然无法让我的具体案例发挥作用。我正在尝试创建一个用于时间序列模型的时间序列对象。我已将数据框中的日期值转换为“日期”列 (%Y-%m-%d) 类型,但我不断收到错误“xts(x1_train[, -1], order.by = x1_train[ 中的错误, 1]) : order.by 需要一个适当的基于时间的对象”。我还通过使用 as.Date 或 as.POSIXct() 转换日期列尝试了其他版本,它给了我以下错误“as.Date.default(x, ...) 中的错误:不知道如何将“x”转换为“日期”类”。

# date convert from CHR to DATE
df$rev_month <- as.Date(df$start_date, "%Y-%m-%d")

# 2 versions of attempts that return errors
x1_train_ts <- xts(x1_train[,-1], order.by=(x1_train[,1])
x1_train_ts <- xts(x1_train[,-1], order.by=as.Date(x1_train[,1], "%Y-%m-%d"))

虽然我确定日期列是日期格式,但两者都不起作用。非常感谢任何方向。

# updated to include test dataframe
test <- data.frame("c_id" = c("none","none","none","none","none",
"none","none","none","none","none","none","none","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101", "none","none","none","none","none","none","none","none","none","none","none","none","none","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111"), "partner_id" = c("1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9"), "rev_month" = as.Date(c("2015-12-01","2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01","2016-06-01","2016-07-01","2016-08-01", "2016-09-01","2016-10-01","2016-11-01","2016-12-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01", "2017-01-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01", "2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01","2019-05-01","2019-06-01","2019-07-01","2019-08-01","2019-09-01","2019-10-01","2019-11-01","2019-12-01", "2020-01-01", "2020-02-01", "2020-03-01")), "rev" = c(101.25, 102.25, 103.50, 103.75, 104.15, 104.25, 104.3, 105.00, 105.20, 105.60, 106.00, 106.10, 106.50, 101.50, 100.30, 107.50, 108.30, 108.45, 109.10, 110.10, 112.15, 112.45, 114.65, 115.00, 116.00, 116.50, 117.25, 117.85, 119.25, 119.95, 120.20, 121.50, 122.30, 122.40, 123.25, 123.75, 124.00, 101.25, 102.25, 103.50, 103.75, 104.15, 104.25, 104.3, 105.00, 105.20, 105.60, 106.00, 106.10, 106.50, 101.50, 100.30, 107.50, 108.30, 108.45, 109.10, 110.10, 112.15, 112.45, 114.65, 115.00, 116.00, 116.50, 117.25, 117.85, 119.25, 119.95, 120.20, 121.50, 122.30, 122.40, 123.25, 123.75, 124.00, 124.10, 125.35, 125.45), stingsAsFactors=FALSE)

【问题讨论】:

    标签: r datetime time-series xts


    【解决方案1】:

    您已经创建了一个日期类的新列rev_monthstart_date 仍然是字符/因子类型。不要创建新列,而是覆盖现有列。

    df$start_date <- as.Date(df$start_date, "%Y-%m-%d")
    

    如果start_date 是您的数据框df 中的第一列,这应该可以工作。

    x1_train_ts <- xts::xts(df[,-1], order.by= df$start_date)
    

    【讨论】:

    • 感谢您的回复。抱歉,我确实覆盖了原始数据框中的日期列,并且可以确认它们之前是日期列类型。 x1_train 只是我的数据帧的单变量时间序列子集,只有收入和日期。这让我很痛苦,哈哈
    • 所以我的回答对你有用,对吧?您的数据框中不应有 rev_month 列。
    • 不,不幸的是它不起作用。仍然给我同样的错误,好像它不能识别正确的日期格式
    • @bbal20 请提供您的数据示例。使用 dput(head(df)) 更新您的帖子。
    • 我刚刚更新了原始帖子以包含示例数据框。此代码是循环的一部分,因此最终每个日期对于特定的 partner_id 都是唯一的,但我只是试图将其分解以修复我的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 2016-08-09
    • 2017-08-19
    • 2019-06-25
    相关资源
    最近更新 更多