Get groups in time series with categorical data in R for use in gts I have sales data organised in a table with 6 columns (4 for the location and type data, and 2 for the dates and the quantity sold), and 24 rows for each category representing the sales over 24 months (there are 7104 rows, 24 for each of the 296 possibilities I have).
I want to use hts.gts, but I need to create the group hierarchy and to change the format of the data for that. What would be the best way to do that in R?
Additionally, is there a better way to store this data?
EDIT: Here is how the data looks:
   PERIOD              PRODUCT.TYPE       BRAND      SIZE   CITY        VALUE
1: 2012-01             A                  X          100    SOFIA       0.11134739
2: 2012-02             A                  X          100    SOFIA       0.02486429
3: 2012-03             A                  X          100    SOFIA       0.09738088
4: 2012-04             A                  X          100    SOFIA       0.11401330
5: 2012-05             A                  X          100    SOFIA       0.15660395
6: 2012-06             A                  X          100    SOFIA       0.21496220

I have a bunch of different products types, each having a different amount of brands and sizes, and which are not all being sold in every city.
 A: In order to use the hts package, the data input should be an mts object. The data above seems a data.frame or a data.table and you need to convert it to a matrix form that contains 296 columns (due to 296 possibilities) and 24 rows (2-year monthly data). The input looks like the following
set.seed(1234)
dat <- ts(matrix(rnorm(7104), nrow = 24, ncol = 296), start = c(2012, 1),
          frequency = 12)

Because you've got the relevant grouping information, paste the columns PRODUCT.TYPE, BRAND, SIZE and CITY together without space using paste or paste0 functions to get a new variable saying blevels referring to the bottom-level labels and assign blevels to the column names of dat.
colnames(dat) <- blevels

Assuming blevels has characters of same length, the argument characters in the gts function can be used.
library(hts)
datgts <- gts(dat, characters = c(1, 1, 3, 5))

Rob's post you mentioned above gives the detailed explanation on how to construct a gts object, if there's more complicated structure.
Hope this helps to get it work. :)
