将不规则间隔的数据转换为时间序列

时间:2018-10-01 10:13:24

标签: r dataframe xts

我有一段时间的销售数据,我想将数据转换为时间序列以进行与时间序列相关的分析。但我仍停留在第一步,请建议如何进行。 以下是我的交易数据,order_date,total_amount和数量。我的订单日期是随机的(间隔不均匀)。

> sku_top_02
         ord_date total_amount qty
36015  2014-01-02       379.81   1
36022  2014-01-02       610.87   2
36050  2014-01-03       289.17   6
36081  2014-01-03       183.12   1
36128  2014-01-06       303.57  10
36193  2014-01-06        51.65   1
36259  2014-01-07       250.31   1
36222  2014-01-08       408.58   1
36264  2014-01-09       183.40   1
36347  2014-01-09       504.90   1
36323  2014-01-13       529.95   1
36412  2014-01-13       204.96   1
36455  2014-01-14       524.83   5
36504  2014-01-14      3771.41  25
36762  2014-01-20       759.86   2
36794  2014-01-21       539.88   2
36826  2014-01-22       599.34   1
37056  2014-01-22       133.35   3
37076  2014-01-22       174.25   4
...
...
...
Please ignore the first column (rownames, after sorting by order date it is jumbled). Below, I am using xts() to convert the data into time-series.

> ts.sku_02 <- xts(df = sku_top_02[,c('total_amount', 'qty')], order.by = sku_top_02$ord_date)

在我的转换中无法正常工作

> ts.sku_02
Data:
numeric(0)

Index:
 Date[1:4386], format: "2014-01-02" "2014-01-02" "2014-01-03" "2014-01-03" "2014-01-06" "2014-01-06" "2014-01-07" "2014-01-08" "2014-01-09" "2014-01-09" ...
> dim(ts.sku_02)
NULL
> str(ts.sku_02)
An 'xts' object of zero-width

此外,我无法绘制TS。请建议如何进行。 预先感谢。

1 个答案:

答案 0 :(得分:1)

假设输入数据帧在末尾的注释中可重复显示:

SELECT `doc_assign`.`id` as doc_assign_id, 
       `user_profiles`.`data` as report_fixed_template_discount, 
       `doc_assign`.`type` as document_type
FROM (`doc_assign`) 
JOIN `jobs` ON `jobs`.`id` = `job_assign`.`job_id`
JOIN `docs` ON `docs`.`id` = `doc_assign`.`doc_id`
LEFT JOIN `user_profiles` ON `user_profiles`.`user_id` = `jobs`.`client_id`
AND `user_profiles`.`meta` =  'report_fixed_template_discount' -- Here!
WHERE `jobs`.`status` =  3
AND `jobs`.`completed_on` >= '2018-09-01 00:00:00'
AND `jobs`.`completed_on` <= '2018-09-30 23:59:59' 

给予:

library(xts)
x <- xts(DF[-1], DF[[1]])

注意

> head(x)
           total_amount qty
2014-01-02       379.81   1
2014-01-02       610.87   2
2014-01-03       289.17   6
2014-01-03       183.12   1
2014-01-06       303.57  10
2014-01-06        51.65   1