【问题标题】:Prophet Date Format R先知日期格式 R
【发布时间】:2020-09-04 05:14:41
【问题描述】:
year_month   amount_usd
201501       -390217.24
201502       230944.09
201503       367259.69
201504       15000.00
201505       27000.21
201506       38249.65

df <- structure(list(year_month = 201501:201506, amount_usd = c(-390217.24, 
230944.09, 367259.69, 15000, 27000.21, 38249.65)), class = "data.frame", row.names = c(NA, 
-6L))

我想将其转换为 DD/MM/YYYY 格式,以便在 Prophet 预测代码中使用。

这是我迄今为止尝试过的。

for (loopitem in loopvec){
  df2 <- subset(df, account_id==loopitem)
  df3 <- df2[,c("year_month","amount_usd")]
  df3$year_month <- as.Date(df3$year_month, format="YYYY-MM", origin="1/1/1970")
  try <- prophet(df3, seasonality.mode = 'multiplicative')
}

fit.prophet(m, df, ...) 中的错误: 数据框必须有列 'ds' 和 'y' 分别包含日期和值。

【问题讨论】:

  • 请编辑您的标题(全部大写),并将您的示例数据粘贴为文本而不是图像。

标签: r date-format facebook-prophet


【解决方案1】:

您需要将日期数字(我只是使用第一个)粘贴到 year_month 值,然后可以使用 lubridate 中的 ymd() 函数将该列转换为日期对象。

library(dplyr)
library(lubridate)

mutate_at(df, "year_month", ~ymd(paste(., "01")))

  year_month amount_usd
1 2015-01-01 -390217.24
2 2015-02-01  230944.09
3 2015-03-01  367259.69
4 2015-04-01   15000.00
5 2015-05-01   27000.21
6 2015-06-01   38249.65

【讨论】:

  • 预言家想要一个 dsy 列,因此您需要更改列名才能使其正常工作。
猜你喜欢
  • 1970-01-01
  • 2018-09-06
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
相关资源
最近更新 更多