首页 > 解决方案 > 合并数据框以创建分组时间序列

问题描述

我想创建一个分组的时间序列(层次结构)。基本上每年有四个季度和 12 个月。年份是总计,季度是月份的小计。月份是最低值。我正在尝试使用Robert Handyman()gts的库:在这里,但我在某处遗漏了一些东西。初步演示数据如下所示。htsforecast

library(tidyr)

表格1:

ts(rnorm(67),  start=c(2015, 4),frequency=12)

表2:

aggregate(ts(rnorm(67),  start=c(2015, 4), frequency=12), nfrequency=4)

任何建议如何将列创建为年度总计,然后按季度汇总,并表示月份。这些本质上意味着,数据集上有年-季-月的层次结构。

标签: rmergetime-series

解决方案


如果您不喜欢坚持该ts()格式,您可以将其放入数据集并使用 , 中的函数zoolubridate进行dplyr所需的输出。

library(dplyr)
library(zoo)
library(tibble)
library(lubridate)
x <- ts(rnorm(67),  start=c(2015, 4),frequency=12)
dat <- tibble(
  # change time of the ts object into year-month 
  # and extract just the month 
  m = month(as.yearmon(time(x))), 
  # change time of the ts object to year-month
  # and extract the year
  year = year(as.yearmon(time(x))), 
  # change time of the ts object to year-month
  # and extract the quarter
  qtr = quarter(as.yearmon(time(x))),
  value = as.vector(x)) %>% 
  # group data by year and quarter
  group_by(year, qtr) %>% 
  # get the sum of value for each year-quarter.
  mutate(qtr_sum = sum(value)) %>% 
  # ungroup the data and then group again just by year
  ungroup() %>% 
  group_by(year) %>% 
  # calculate the yearly sum 
  mutate(yr_sum = sum(value))

dat
# # A tibble: 67 x 6
# # Groups:   year [6]
#     m   year   qtr   value qtr_sum yr_sum
#   <dbl> <dbl> <int>   <dbl>   <dbl>  <dbl>
# 1     4  2015     2  0.555  -1.68   -0.851
# 2     5  2015     2 -0.767  -1.68   -0.851
# 3     6  2015     2 -1.47   -1.68   -0.851
# 4     7  2015     3 -1.75   -0.0733 -0.851
# 5     8  2015     3  1.26   -0.0733 -0.851
# 6     9  2015     3  0.408  -0.0733 -0.851
# 7    10  2015     4 -0.0731  0.905  -0.851
# 8    11  2015     4  0.824   0.905  -0.851
# 9    12  2015     4  0.154   0.905  -0.851
# 10     1  2016     1 -0.231  -0.634   2.70 
# # … with 57 more rows


推荐阅读