I have a time series in the classical form of (dput(data,30) output at the end of the question)
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
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.