# hts in R: Convert data.frame into a gts object [closed]

I'm a month-old user of RStudio. Currently, I'm working with hierarchical time series analysis with the hts package. After reading my .csv data below,

      WEEKNUM     SITE   OPERATIONS    ATTRITION
1    201502      DAVAO   PRODUCTION         0
2    201503      DAVAO   PRODUCTION         3
3    201504      DAVAO   PRODUCTION         8
4    201505      DAVAO   PRODUCTION         6
5    201506      DAVAO   PRODUCTION         9
6    201507      DAVAO   PRODUCTION         4
7    201508      DAVAO   PRODUCTION         9
...
592  201623      WM      TRAINING           0


I've tried creating the matrix out of it with these codes:

Then a matrix was successfully created, however the values were rnorm generated:

How do I replace them with the original grouped series values?

Or how do I start hts better, with my data import?

Thanks and more power! :)

It's not a statistics but a program topic....

Supposition in this example: There are four SITE and two OPERATION. (if not, you'll modify each number in below code.)

# example data as .csv data (you needn't)
df <- data.frame(WEEKNUM=rep(c(201502:201552, 201601:201623), 8),
SITE = rep(LETTERS[1:4], each=74),
OPERATIONS = rep(LETTERS[11:12], each=296),
ATTRITION = rnorm(592))

# If undone, you'll sort the data (1st key is SITE, 2nd is OPERATIONS, last is WEEKNUM).
df <- df[order(df$SITE, df$OPERATIONS, df$WEEKNUM),] df$test <- paste(df$SITE, df$OPERATIONS, seq=" ") # make the row to check

matrix(df$test, nrow=74) # checking. If you succeed, each column consist of one factor. abc <- ts(matrix(df$ATTRITION, nrow=74), start=c(2015,2), frequency=52)   # creating time-series objects
SITE <- rep(levels( df$SITE ), each=2) # each=2; the number of OPERATIONS OPERATIONS <- rep(levels( df$OPERATIONS ), 4) # 4; the number of SITE
gc <- rbind(SITE, OPERATIONS)                 # a matrix consists of strings
rownames(gc) <- c("SITE", "OPERATIONS")
x <- gts(abc, groups = gc)                    # finish

• Thanks crayfish44, but the formed gts object still prints negative values. :( How do I replace them with the original series? Jun 11 '16 at 0:56
• or are they some sort of standardized? Jun 11 '16 at 1:15
• df <- read.csv(your file) and you'll do all lines under # If undone, you~. Jun 11 '16 at 2:47