将几个月的零需求添加到动物园时间序列 [英] Add months of zero demand to zoo time series

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

问题描述

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

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-02-01 到 2013-09-01.

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及其>填充参数.当前数据集与包含所有所需日期的空 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天全站免登陆