r - 先知日期格式 R
问题描述
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”。
解决方案
您需要将天数(我只是使用第一个)粘贴到 year_month 值,然后可以使用ymd()
lubridate 中的函数将该列转换为日期对象。
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
推荐阅读
- javascript - 我无法在我的代码中取消周末
- node.js - 如何将 $match 与条件 if-else 一起使用?
- javascript - 如何使用 jquery 使用 datetimepicker 选择多个日期?
- python-3.x - 为什么我们需要在 sqlite SELECT 语句中的变量后加逗号?
- c# - 如何识别我的代码何时在 C# 中进行测试?
- c# - 如何将 Windows 应用程序部署到较旧的 .NET Framework
- c# - 如何向 ASP.NET 会话追加/添加数据?
- pyspark - substring 函数返回列类型而不是值。有没有办法从 pyspark 中的列类型中获取值
- javascript - 单击 3 次时替换 DIV 元素
- php - 仅在主题设置上排队管理脚本