使用xts
package ,你可以做这样的事情
library(xts)
dat.ts <- xts(x=data[,-1], ## create an xts object
order.by=as.Date(data[,1])) ## coerce the index to date
dat.quart <- apply.quarterly(dat.ts,mean) ## apply for each quarter
要显示一些行:
rbind(head(dat.quart),tail(dat.quart))
tmpd pm10median so2median
1987-03-31 33.60556 NA NA
1987-06-30 62.19231 NA -0.3283393
1987-09-30 71.31522 NA -1.9137842
1987-12-31 41.09783 NA NA
1988-03-31 27.06593 NA NA
1988-06-30 60.48352 NA NA
1999-09-30 71.01087 2.697414 -0.4532943
1999-12-31 42.86957 1.565251 -0.4035715
2000-03-31 34.74725 -4.704813 0.2392453
2000-06-30 59.07692 NA -0.5426823
2000-09-30 69.67391 NA -1.9221470
2000-12-31 36.59783 NA -0.2387025
更新
看起来 OP 想要按季度拆分移动平均线系列。
dat.ts <- xts(x=data[,-1], ## create an xts object
order.by=as.Date(data[,1])) ## coerce the index to date
dat.m <- rollmean(dat.ts,k=2) ## compute the MA
ep <- endpoints(dat.m, "quarters") ## create an index
## this split the seriers by quarter
xx <- sapply(1:(length(ep) - 1), function(y) {
dat.m[(ep[y] + 1):ep[y + 1]]
})