我'有一些月度销售数据x
,我希望将其汇总四个月。当我使用聚合作为四分之一数据aggregate(x, nfrequency = 4, FUN = sum)
时,我会得到一个包含列名Qtr1
,Qtr2
,Qtr3
,Qtr4
和年份作为rownames的漂亮表格。但是,当我将频率更改为nfrequency = 3
时,我只得到一个包含正确值的列表,而不是好表。是否有一种聪明的方法来获得类似的表格,而不是四分之一时期,但是这个三年级值?
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2006 279.95 299.61 442.00 409.94 410.50 403.63 408.54 336.47 378.93 388.13 319.38 377.63
2007 343.37 343.09 450.52 360.06 373.00 497.27 370.10 430.72 313.88 328.64 383.43 214.27
2008 346.37 281.68 325.14 317.50 320.07 375.60 449.38 322.88 231.23 262.45 268.53 187.59
2009 261.36 225.25 299.97 312.90 351.44 298.00 332.04 271.91 183.07 246.60 260.59 127.01
2010 175.45 164.04 313.62 320.35 323.61 344.62 271.85 284.24 230.13 232.94 192.12 112.46
2011 167.15 187.44 178.87 318.83 251.52 230.51 232.49 155.83 180.65 167.19 118.87 137.40
2012 124.81 127.35 172.95 140.92 171.47 208.92 133.56 130.90 119.06 93.34 78.76 79.08
2013 97.49 98.65 117.95 142.32 138.32 118.60 147.08 88.17 91.56 115.47 114.35 100.48
2014 79.54 100.24 120.39 147.05 175.03 114.21 167.29 113.88 94.42 110.28 99.19 65.33
2015 79.44 114.24 144.33 173.30 151.79 142.02
答案 0 :(得分:0)
最后,我找到了这个解决方案。
library(lubridate)
library(plyr)
library(reshape2)
cuatrimestre<-ifelse(cycle(data) %in% c(1:4), 1, ifelse(cycle(data) %in% c(5:8), 2, 3))
year<-as.numeric(substring(time(data), first=1, last=4))
data.cuatrimestre<-as.data.frame(cbind(data, cuatrimestre, year))
data.por.cuatrimestres<-aggregate(data~year+cuatrimestre, data.cuatrimestre, function(x) c(sum=sum(x)))
dcast(data.por.cuatrimestres, year~cuatrimestre, sum)
但是,肯定必须是一种更有效的方法。有人可以帮忙吗?
答案 1 :(得分:0)
使用data.table
,可以通过两行代码完成:
library(data.table)
cm <- data.table(year = as.integer(time(x)), part = (cycle(x) - 1L) %/% 4 + 1L, x)
dcast(cm[, .(sum = sum(x)), keyby = "year,part"], year ~ part)
year 1 2 3
1: 2006 1431.50 1559.14 1464.07
2: 2007 1497.04 1671.09 1240.22
3: 2008 1270.69 1467.93 949.80
4: 2009 1099.48 1253.39 817.27
5: 2010 973.46 1224.32 767.65
6: 2011 852.29 870.35 604.11
7: 2012 566.03 644.85 370.24
8: 2013 456.41 492.17 421.86
9: 2014 447.22 570.41 369.22
10: 2015 511.31 293.81 NA
第一行根据类data.table
的数据x
创建ts
。 (不幸的是,OP忘记在Q中传达这一事实。)
计算年份4
时的分隔符part
会给出该期间的月份数。将此值更改为3
以获取季度结果(3个月期)或6
更改为半年期结果(6个月期)。
第二行计算按年度year
和part
分组的聚合,然后使用dcast将结果从长格式转换为宽格式。
为了使此解决方案可重现,必须提供数据。不幸的是,OP没有提供dput
数据,而是一张表,它是将x
打印为ts
对象的结果。
使用fread
中包含的通用data.table
函数(使用的开发版本1.9.7)读取(略微修改的)表。然后将它从宽格式转换为长格式(melt
),得到一个向量。在一些日期计算和排序之后,最终创建了类ts
的时间序列。
library(data.table)
dt <- fread("year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2006 279.95 299.61 442.00 409.94 410.50 403.63 408.54 336.47 378.93 388.13 319.38 377.63
2007 343.37 343.09 450.52 360.06 373.00 497.27 370.10 430.72 313.88 328.64 383.43 214.27
2008 346.37 281.68 325.14 317.50 320.07 375.60 449.38 322.88 231.23 262.45 268.53 187.59
2009 261.36 225.25 299.97 312.90 351.44 298.00 332.04 271.91 183.07 246.60 260.59 127.01
2010 175.45 164.04 313.62 320.35 323.61 344.62 271.85 284.24 230.13 232.94 192.12 112.46
2011 167.15 187.44 178.87 318.83 251.52 230.51 232.49 155.83 180.65 167.19 118.87 137.40
2012 124.81 127.35 172.95 140.92 171.47 208.92 133.56 130.90 119.06 93.34 78.76 79.08
2013 97.49 98.65 117.95 142.32 138.32 118.60 147.08 88.17 91.56 115.47 114.35 100.48
2014 79.54 100.24 120.39 147.05 175.03 114.21 167.29 113.88 94.42 110.28 99.19 65.33
2015 79.44 114.24 144.33 173.30 151.79 142.02 NA NA NA NA NA NA")
mdt <- melt(dt, id.vars = "year")[, dates := lubridate::ymd(paste(year, variable, "01"))]
x <- ts(mdt[order(dates)][!is.na(value), value], start = c(2006L, 1L), frequency = 12)