动物园时间序列增加零需求月份 [英] Add months of zero demand to zoo time series

查看:119
本文介绍了动物园时间序列增加零需求月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些间歇性的需求数据,其中仅包括存在需求的行.我通过read.csv引入它,我的2列是日期(作为日期)和数量(作为整数).然后,我将其转换为动物园系列,并将每日需求组合为每月需求.我的最终输出是一个动物园系列,日期是该月的第一天,该月的总需求量.

I have some intermittent demand data that only includes lines where demand is present. I bring it in via read.csv, and my 2 columns are Date (as date) and Quantity (as integer). Then I convert it to a zoo series and combine the daily demand into monthly demand. My final output is a zoo series with the date being the first day of the month and the summed demand for that month.

我的问题是,这个动物园系列缺少需求为零的月份之间的时间,因此我需要这些来正确预测间歇性需求.

My problem is that this zoo series is missing the in between months that have zero demand and I need these to forecast intermittent demand correctly.

例如:我在日期2013-01-01中有数量2,然后下一行是2013-10-01中的数量3.我需要在2013年2月1日至2013年9月1日之间添加零数量.

For example: I have quantity 2 in date 2013-01-01 and then the next line is quantity 3 in 2013-10-01. I need to add quantity zero to 2013-02-01 through 2013-09-01.

Date <- c('1/1/2013','10/1/2013','11/1/2013')
Quantity <- c('2','3','6')

Date <- as.Date(Date, "%m/%d/%Y")

df <- data.frame(Date, Quantity)
df <- read.zoo(df)
df

动物园系列输出:

2013-01-01  2013-10-01  2013-11-01
         2           3           6

推荐答案

由于"df"是zoo对象,因此可以使用merge.zoo及其fill参数.当前数据集与一个空的zoo对象合并,该对象包含所有期望的日期.

Because "df" is a zoo object, you may use merge.zoo and its fill argument. The current data set is merged with an empty zoo object which contains all the desired dates.

tt <- seq(min(Date), max(Date), "month")
merge(df, zoo(, tt), fill = 0)

# 2013-01-01 2013-02-01 2013-03-01 2013-04-01 2013-05-01 2013-06-01 2013-07-01 2013-08-01 2013-09-01 2013-10-01 2013-11-01 
#          2          0          0          0          0          0          0          0          0          3          6 

有关其他示例,请参见?merge.zoo(将不规则序列扩展为规则序列").

For further examples, see ?merge.zoo ("extend an irregular series to a regular one").

这篇关于动物园时间序列增加零需求月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆