Transforming a time-series into a data frame and back

前端 未结 3 920
夕颜
夕颜 2020-12-01 16:08

The output of a time-series looks like a data frame:

ts(rnorm(12*5, 17, 8), start=c(1981,1), frequency = 12)

       Jan       Feb       Mar       Apr                


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-01 17:02

    Here are two ways. The first way creates dimnames for the matrix about to be created and then strings out the data into a matrix, transposes it and converts it to data frame. The second way creates a by list consisting of year and month variables and uses tapply on that later converting to data frame and adding names.

    # create test data
    set.seed(123)
    tt <- ts(rnorm(12*5, 17, 8), start=c(1981,1), frequency = 12)
    

    1) matrix. This solution requires that we have whole consecutive years

    dmn <- list(month.abb, unique(floor(time(tt))))
    as.data.frame(t(matrix(tt, 12, dimnames = dmn)))
    

    If we don't care about the nice names it is just as.data.frame(t(matrix(tt, 12))) .

    We could replace the dmn<- line with the following simpler line using @thelatemail's comment:

    dmn <- dimnames(.preformat.ts(tt))
    

    2) tapply. A more general solution using tapply is the following:

    Month <-  factor(cycle(tt), levels = 1:12, labels = month.abb)
    tapply(tt, list(year = floor(time(tt)), month = Month), c)
    

    Note: To invert this suppose X is any of the solutions above. Then try:

    ts(c(t(X)), start = 1981, freq = 12)
    

    Update

    Improvement motivated by comments of @latemail below.

提交回复
热议问题