【问题标题】:Convert a data frame with Product_Type, Date and Demand to timeseries object?将具有 Product_Type、Date 和 Demand 的数据框转换为 timeseries 对象?
【发布时间】:2018-08-15 19:39:25
【问题描述】:
Product_Code    Date    Order_Demand
Product_1904    09-01-2017  4000
Product_0250    09-01-2017  148
Product_0471    09-01-2017  30
Product_1408    06-01-2017  1000
Product_0689    06-01-2017  200
Product_0689    06-01-2017  300
Product_1926    06-01-2017  2
Product_1938    06-01-2017  20

我是 R 新手。我想将上述数据转换为时间序列对象 ts,这样行名将是 Product_Code,列名将是月或季度。请帮帮我!!

【问题讨论】:

  • 请用您的数据或类似数据添加一个可重现的示例
  • 抱歉,我无法正确复制粘贴表格,但第一列为“Product_Code”,第二列为“Date”,第三列为“Demand”。请帮助如何将这样的表转换为 ts(时间序列)对象

标签: r time-series forecasting


【解决方案1】:

我认为这应该适合你,

library(xts)
library(lubridate)

# dummmy data
test_data <- data.frame(
  Product_Code = c("Product_1904","Product_0250","Product_0471"),
  Date = mdy(c("09-01-2017","09-02-2017","09-03-2017")),
  Order_Demand = c(4000,148,30)
)

# convert dummy data into xts time series
xts::xts(test_data, order.by = test_data$Date) -> time_series_data

str(time_series_data)

An ‘xts’ object on 2017-09-01/2017-09-03 containing:
  Data: chr [1:3, 1:3] "Product_1904" "Product_0250" "Product_0471" "2017-09-01" "2017-09-02" "2017-09-03" "4000" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "Product_Code" "Date" "Order_Demand"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL

从下次开始,请使用dput()并从R终端复制粘贴结果以提供数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多