简体   繁体   English

将时间序列数据有效转换为R中hts包所需的结构

[英]Efficient conversion of time series data into structure required for hts package in R

I have a time series in the classical form of (dput(data,30) output at the end of the question) 我有一个古典形式的时间序列(问题末尾的dput(data,30)输出)

    Region Country Channel Customer Family Product Pack Date       Quantity
    A      a       1       Z        A      a       1    2011-11-01 1000
    ...
    A      a       1       Z        A      a       1    2014-11-01 2000
    A      a       1       Z        A      a       2    2011-11-01 1000
    ...
    A      a       1       Z        A      a       2    2014-11-01 1000
    A      a       1       Z        A      b       1    2011-11-01 1000
    ...
    A      a       1       Z        A      b       2    2014-11-01 1000
    ...
    ...
    D      g       4       P        D      q       4    2011-11-01 1000

    ...
    D      g       4       P        D      q       4    2014-11-01 1000

I am struggling to find an efficient way of munging this data into the structure required by gts/hts, namely 我正在努力寻找一种有效的方法来将这些数据整合到gts / hts所需的结构中,即

Date       Aa1ZAa1 Aa1ZAa2 Aa1ZAa3 ... Aa1ZAb1 Aa1ZBa1 ... ... Dg4PDq4 
2011-11-01 1000    1000    234         654     354345          1234
...
2014-11-01 2000    1000    345         3454    345443          334

I'm using iteration at the moment which is clearly quite slow. 我现在正在使用迭代,这显然很慢。

I also have the problem that not all the series are the same length because some new products have been introduced or existing products have been sold into new countries/channels. 我还遇到的问题是,由于引入了一些新产品或现有产品已经销售到新的国家/地区,因此并非所有系列的长度都相同。

All help greatly appreciated. 所有帮助,不胜感激。

Regards Trevor 问候特雷弗

structure(list(Customer = c("bci", "bci", "bci", "bci", "bci", 
"bci", "bci", "bci", "bci", "bci", "bci", "bci", "bci", "bci", 
"bci", "bci", "bci", "bci", "bci", "bci", "bci", "bci", "bci", 
"bci", "bci", "bci", "bci", "bci", "bci", "bci"), Site = c("SOP030", 
"SOP030", "SOP030", "SOP030", "SOP030", "SOP030", "SOP030", "SOP030", 
"SOP030", "SOP030", "SOP030", "SOP030", "SOP030", "SOP030", "SOP030", 
"SOP030", "SOP030", "SOP030", "SOP030", "SOP030", "SOP030", "SOP030", 
"SOP030", "SOP030", "SOP030", "SOP030", "SOP030", "SOP030", "SOP030", 
"SOP030"), Item = c("System", "System", "System", "System", "System", 
"System", "System", "System", "System", "System", "System", "System", 
"System", "System", "System", "System", "System", "System", "System", 
"System", "System", "System", "System", "System", "System", "System", 
"System", "System", "System", "System"), Part = c("Gamer", "Gamer", 
"Gamer", "Gamer", "Gamer", "Gamer", "Gamer", "Gamer", "Gamer", 
"Gamer", "Gamer", "Gamer", "Gamer", "Gamer", "Gamer", "Server", 
"Server", "Server", "Server", "Server", "Server", "Server", "Server", 
"Server", "Server", "Server", "Server", "Server", "Server", "Server"
), Date = structure(c(15765, 15796, 15826, 15859, 15887, 15918, 
15950, 15979, 16010, 16041, 16071, 16104, 16132, 16161, 16191, 
15765, 15796, 15826, 15859, 15887, 15918, 15950, 15979, 16010, 
16041, 16071, 16104, 16132, 16161, 16191), class = "Date"), Qty = c(735, 
0, 0, 665, 0, 693, 735, 770, 784, 805, 777, 763, 728, 749, 714, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Customer", 
"Site", "Item", "Part", "Date", "Qty"), row.names = c(65L, 66L, 
67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 
94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 
106L, 107L, 108L), class = "data.frame")

If df is the dataset, you can try 如果df是数据集,则可以尝试

library(reshape2)
df1 <- data.frame(Multcol=as.character(interaction(df[,1:4]),sep=''),
                                df[,5:6], stringsAsFactors=FALSE)
res <- dcast(df1, Date~Multcol, value.var='Qty')
head(res,3)
#         Date bci.SOP030.System.Gamer bci.SOP030.System.Server
#1 2013-03-01                     735                        0
#2 2013-04-01                       0                        0
#3 2013-05-01                       0                        0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM