Aggregate time series
stats-aggregate.Rd
Aggregate a "timeSeries"
object over general periods.
Arguments
- x
an object of class
"timeSeries"
.- by
a sequence of
"timeDate"
objects denoting the aggregation periods, see section ‘Details’.- FUN
the function to be applied.
- ...
arguments passed to other methods.
Details
aggregate
aggregates x
by applying FUN
on the
values of the time series in each of the aggregation periods,
specified by argument by
.
Argument by
should be of the same class as
time(x)
. by
is sorted and duplicated values are removed
from it. Each pair of consecutive values in by
then determines
a period over which to apply the aggregation function FUN
, see
findInterval
.
Examples
## Load Microsoft Data Set -
x <- MSFT
## Aggregate by Weeks -
by <- timeSequence(from = start(x), to = end(x), by = "week")
aggregate(x, by, mean)
#> GMT
#> Open High Low Close Volume
#> 2000-09-27 63.43750 63.56250 59.81250 60.62500 53077800
#> 2000-10-04 59.65000 60.07500 57.70000 58.55000 40680380
#> 2000-10-11 54.97500 56.45000 54.16250 55.08750 36448900
#> 2000-10-18 53.03750 54.25000 50.83750 52.13750 50631280
#> 2000-10-25 61.78750 64.18750 60.08750 62.38750 86457340
#> 2000-11-01 66.13750 68.78750 65.85000 67.93750 53496000
#> 2000-11-08 69.83750 70.96876 68.68750 69.60000 53202820
#> 2000-11-15 68.43750 70.08750 67.13124 68.71250 41280860
#> 2000-11-22 68.08750 69.75000 67.13438 68.23750 41464220
#> 2000-11-29 69.15625 69.89062 67.29688 68.17188 43184175
#> 2000-12-06 59.30000 60.45000 56.51250 57.40000 57971300
#> 2000-12-13 56.37500 57.82500 54.85000 56.25000 52329080
#> 2000-12-20 49.64688 50.54688 47.20000 47.76250 56459820
#> 2000-12-27 44.62500 46.90625 43.98438 45.79688 51083775
#> 2001-01-03 44.09375 46.48438 43.21875 44.81250 49496525
#> 2001-01-10 49.25000 51.31250 48.32500 50.23750 49082480
#> 2001-01-17 53.71875 54.78125 52.12500 53.50000 39609175
#> 2001-01-24 59.03750 60.60000 58.03750 60.02500 57656080
#> 2001-01-31 62.96250 64.28750 61.98750 62.95000 40289680
#> 2001-02-07 61.62500 63.38750 60.97500 62.47500 41679580
#> 2001-02-14 60.22500 61.11250 58.43750 59.33750 39770480
#> 2001-02-21 57.15625 58.68750 56.14062 57.06250 32158025
#> 2001-02-28 57.46250 59.10312 56.48438 57.97500 46513080
#> 2001-03-07 58.36250 59.47500 57.38750 58.72188 33749140
#> 2001-03-14 55.52500 56.75626 53.82500 55.21250 45451820
#> 2001-03-21 53.82500 55.21562 52.29688 53.06250 46233500
#> 2001-03-28 55.21250 57.01250 54.33750 56.08750 46281860
#> 2001-04-04 54.92500 56.12500 53.37500 54.23750 45234480
#> 2001-04-11 57.05900 58.71450 56.34050 57.96150 48136060
#> 2001-04-18 61.21750 63.07750 60.62750 62.47000 49402875
#> 2001-04-25 67.99800 69.65800 67.10800 68.50600 61038700
#> 2001-05-02 69.35800 70.23800 67.81800 68.78600 49124540
#> 2001-05-09 70.21400 71.35600 69.48200 70.62400 44692860
#> 2001-05-16 69.33200 69.92400 68.44800 69.11000 31371080
#> 2001-05-23 68.93600 70.25600 68.24800 69.01200 47817480
#> 2001-05-30 70.49000 71.50250 69.58250 70.54000 36405225
#> 2001-06-06 70.65800 71.73200 69.79000 71.05200 34148380
#> 2001-06-13 72.34800 73.00800 71.41800 72.35200 28818960
#> 2001-06-20 68.20600 69.05000 66.95200 68.10600 36473940
#> 2001-06-27 69.18600 70.54200 68.46600 69.76000 30218820
#> 2001-07-04 71.62500 73.37750 70.50250 71.70250 40513375
#> 2001-07-11 66.96600 67.80600 65.54000 66.24800 32357220
#> 2001-07-18 70.96200 71.94400 70.28600 71.30200 36383520
#> 2001-07-25 68.35000 69.43000 67.36400 68.52800 42234800
#> 2001-08-01 66.32600 66.93000 65.54000 66.10400 30027700
#> 2001-08-08 66.71800 67.26200 65.68400 66.33600 21163460
#> 2001-08-15 65.09000 65.70800 63.92000 64.85000 20595220
#> 2001-08-22 62.42200 63.18800 61.01800 62.12800 26973000
#> 2001-08-29 61.11200 62.28400 59.98400 60.89400 25536780
#> 2001-09-05 57.31500 58.79750 56.07000 56.95750 39024075
#> 2001-09-12 55.86333 57.90000 55.30333 56.33333 47782067
#> 2001-09-19 53.96333 54.93333 52.19000 53.70000 56272467
#> 2001-09-26 50.94000 52.09200 49.55000 50.81000 53200500
## Aggregate to Last Friday of Month -
by <- unique(timeLastNdayInMonth(time(x), 5))
X <- aggregate(x, by, mean)
X
#> GMT
#> Open High Low Close Volume
#> 2000-09-29 61.75000 62.25000 59.68750 60.75000 38761600
#> 2000-10-27 57.55625 59.29375 56.00625 57.56562 56372100
#> 2000-11-24 68.72697 70.21875 67.74095 68.96053 44467347
#> 2000-12-29 54.24154 55.53841 52.34635 53.37240 53435483
#> 2001-01-26 53.74306 55.60417 52.71528 54.57986 49482550
#> 2001-02-23 59.98355 61.29934 58.79934 59.90461 39193716
#> 2001-03-30 56.09250 57.47500 54.87625 56.13688 43340872
#> 2001-04-27 61.75026 63.26855 60.66579 62.08382 52669442
#> 2001-05-25 69.56000 70.59350 68.75750 69.70200 40581970
#> 2001-06-29 70.27083 71.39792 69.32333 70.48417 34949438
#> 2001-07-27 68.78474 69.65368 67.71474 68.60632 35630868
#> 2001-08-31 63.64240 64.44040 62.52000 63.33960 25102488
#> 2001-09-28 53.40357 54.79357 52.12071 53.42500 49792257
dayOfWeek(time(X))
#> 2000-09-29 2000-10-27 2000-11-24 2000-12-29 2001-01-26 2001-02-23 2001-03-30
#> "Fri" "Fri" "Fri" "Fri" "Fri" "Fri" "Fri"
#> 2001-04-27 2001-05-25 2001-06-29 2001-07-27 2001-08-31 2001-09-28
#> "Fri" "Fri" "Fri" "Fri" "Fri" "Fri"
isMonthly(X)
#> [1] TRUE
## Aggregate to Last Day of Quarter -
by <- unique(timeLastDayInQuarter(time(x)))
X <- aggregate(x, by, mean)
X
#> GMT
#> Open High Low Close Volume
#> 2000-09-30 61.75000 62.25000 59.68750 60.75000 38761600
#> 2000-12-31 59.66245 61.15799 58.15104 59.40476 51663067
#> 2001-03-31 56.60282 58.10383 55.45111 56.83947 43853037
#> 2001-06-30 67.47548 68.69083 66.53270 67.70242 42081671
#> 2001-09-30 62.85552 63.81966 61.71155 62.67172 34511040
isQuarterly(X)
#> [1] TRUE