1) If DF
is the input data.frame convert it to a zoo object z
with a "yearmon"
index and then aggregate that to "yearqtr"
:
library(zoo)
toYearmon <- function(y, m) as.yearmon(paste(y, m, sep = "-"))
z <- read.zoo(DF, index = 2:1, FUN = toYearmon)
ag <- aggregate(z, as.yearqtr, sum)
giving:
> ag
2004 Q1 2004 Q2 2004 Q3 2004 Q4
875 820 785 608
2) This would also work:
library(zoo)
yq <- as.yearqtr(as.yearmon(paste(DF$Year, DF$Month), "%Y %m"))
ta <- tapply(DF$DepressionCount, yq, sum)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…