您可以从日期中提取“分组变量”,例如年和月,然后对不同的值使用聚合函数。那将是手工完成。
或者你可以使用已经有操作符的xts包:
R> library(quantmod) ## for getSymbols()
R> SPY <- getSymbols("SPY", auto.assign=FALSE) ## SPY is now of class xts
我们可以看看数据
R> summary(SPY)
Index SPY.Open SPY.High SPY.Low
Min. :2007-01-03 Min. : 68 Min. : 70 Min. : 67.1
1st Qu.:2008-07-03 1st Qu.:111 1st Qu.:112 1st Qu.:110.0
Median :2010-01-04 Median :128 Median :129 Median :127.5
Mean :2010-01-02 Mean :124 Mean :125 Mean :123.0
3rd Qu.:2011-07-05 3rd Qu.:140 3rd Qu.:140 3rd Qu.:139.0
Max. :2013-01-04 Max. :157 Max. :158 Max. :155.4
SPY.Close SPY.Volume SPY.Adjusted
Min. : 68.1 Min. :3.87e+07 Min. : 62.6
1st Qu.:110.8 1st Qu.:1.38e+08 1st Qu.:104.1
Median :128.4 Median :1.86e+08 Median :121.1
Mean :124.0 Mean :2.12e+08 Mean :116.1
3rd Qu.:139.7 3rd Qu.:2.57e+08 3rd Qu.:130.0
Max. :156.5 Max. :8.71e+08 Max. :146.4
R>
并运行我们想要的计算:
R> tail(SPY[ endpoints(SPY) ])
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume
2012-08-31 141.29 141.82 140.36 141.16 151970400
2012-09-28 144.09 144.56 143.46 143.97 150696100
2012-10-31 141.85 142.03 140.68 141.35 103438500
2012-11-30 142.14 142.42 141.66 142.15 136568300
2012-12-31 139.66 142.56 139.54 142.41 243935200
2013-01-04 145.97 146.61 145.67 146.37 116790800
SPY.Adjusted
2012-08-31 139.42
2012-09-28 142.96
2012-10-31 140.35
2012-11-30 141.15
2012-12-31 142.41
2013-01-04 146.37
这endpoints()
是您想要的功能,它默认选择月份。它找到我们想要的行索引。所以多年来一直如此:
R> SPY[ endpoints(SPY, "years") ]
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume
2007-12-31 147.10 147.61 146.06 146.21 108126800
2008-12-31 89.08 90.97 88.87 90.24 193987200
2009-12-31 112.77 112.80 111.39 111.44 90637900
2010-12-31 125.53 125.87 125.33 125.75 91218900
2011-12-30 126.02 126.33 125.50 125.50 95599000
2012-12-31 139.66 142.56 139.54 142.41 243935200
2013-01-04 145.97 146.61 145.67 146.37 116790800
SPY.Adjusted
2007-12-31 131.14
2008-12-31 82.88
2009-12-31 104.73
2010-12-31 120.49
2011-12-30 122.78
2012-12-31 142.41
2013-01-04 146.37
R>